Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

void Main() {
  int L = 10000000;
  int LL = 999999999;
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> s(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char a;
      cin >> a;
      s[i][j] = (a == '#' ? LL : L);
    }
  }

  deque<pair<int, int>> dq;
  dq.push_front(make_pair(ch, cw));
  s[ch][cw] = 0;
  while (!dq.empty()) {
    auto q = dq.front();
    dq.pop_front();
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (q.first + i >= 0 && q.first + i < h && q.second + j >= 0 &&
            q.second + j < w && s[q.first + i][q.second + j] != LL &&
            (i != 0 || j != 0)) {
          int ac = abs(i) + abs(j) <= 1 ? 0 : 1;
          if (s[q.first + i][q.second + j] > s[q.first][q.second] + ac) {
            if (ac == 0)
              dq.push_front(make_pair(q.first + i, q.second + j));
            else
              dq.push_back(make_pair(q.first + i, q.second + j));
            s[q.first + i][q.second + j] = s[q.first][q.second] + ac;
          }
        }
      }
    }
  }

  int ans = s[dh][dw];
  if (ans == L)
    ans = -1;
  cout << ans << endl;

  // .#.
  // #@#
  // .#.
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void Main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> s(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char a;
      cin >> a;
      s[i][j] = a == '#' ? -1 : 0;
    }
  }

  queue<pair<int, int>> q;
  int ind = 1;
  stack<pair<int, int>> st;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 0) {
        st.push(make_pair(i, j));
        while (!st.empty()) {
          auto p = st.top();
          st.pop();
          int x = p.second;
          int y = p.first;
          s[y][x] = ind;
          if (y > 0 && s[y - 1][x] == 0) {
            st.push(make_pair(y - 1, x));
            continue;
          }
          if (y < h - 1 && s[y + 1][x] == 0) {
            st.push(make_pair(y + 1, x));
            continue;
          }
          if (x > 0 && s[y][x - 1] == 0) {
            st.push(make_pair(y, x - 1));
            continue;
          }
          if (x < w - 1 && s[y][x + 1] == 0) {
            st.push(make_pair(y, x + 1));
            continue;
          }
        }
        ind++;
      }
    }
  }

  vector<vector<int>> path(ind, vector<int>(ind, 10000000));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == -1)
        continue;
      for (int k = 0; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          if (s[i][j] > 0 && i + k >= 0 && i + k < h && j + l >= 0 &&
              j + l < w && s[i + k][j + l] > 0) {
            path[s[i][j]][s[i + k][j + l]] = 1;
            path[s[i + k][j + l]][s[i][j]] = 1;
          }
        }
      }
    }
  }
  for (int i = 0; i < ind; i++) {
    path[i][i] = 0;
  }

  for (int k = 0; k < ind; k++) {     // 経由する頂点
    for (int i = 0; i < ind; i++) {   // 始点
      for (int j = 0; j < ind; j++) { // 終点
        path[i][j] = min(path[i][j], path[i][k] + path[k][j]);
      }
    }
  }

  int cp = s[ch][cw];
  int dp = s[dh][dw];
  int ans = path[cp][dp];
  if (ans == 10000000)
    ans = -1;

  cout << ans << endl;

  // .#.
  // #@#
  // .#.
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  Main();
  return 0;
}