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

#include <bits/stdc++.h>
#define MAX 1000
using namespace std;
int n, m;
string v[MAX + 1];
int sp[MAX + 1][MAX + 1];
int movex[] = {-1, +1, 0, 0};
int movey[] = {0, 0, -1, +1};
int a, b;
bool valid(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && v[x][y] == '.');
}
int dijsktra(int x, int y) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      sp[i][j] = INT_MAX;
  set<pair<int, pair<int, int>>> st;
  st.insert({0, {x, y}});
  sp[x][y] = 0;
  int cost;
  while (!st.empty()) {
    pair<int, pair<int, int>> p = *(st.begin());
    x = p.second.first, y = p.second.second, cost = p.first;
    st.erase(st.begin());
    for (int i = 0; i < 4; ++i) {
      int r = x + movex[i], c = y + movey[i];
      if (valid(r, c)) {
        if (sp[r][c] > sp[x][y] + 0) {
          set<pair<int, pair<int, int>>>::iterator it =
              st.find({sp[r][c], {r, c}});
          if (it != st.end())
            st.erase(it);
          sp[r][c] = sp[x][y];
          st.insert({sp[r][c], {r, c}});
        }
      }
    }
    for (int r = x - 2; r <= x + 2; ++r) {
      for (int c = y - 2; c <= y + 2; ++c) {
        if (valid(r, c)) {
          if (sp[r][c] > sp[x][y] + 1) {
            set<pair<int, pair<int, int>>>::iterator it =
                st.find({sp[r][c], {r, c}});
            if (it != st.end())
              st.erase(it);
            sp[r][c] = sp[x][y] + 1;
            st.insert({sp[r][c], {r, c}});
          }
        }
      }
    }
  }
  return (sp[a][b] == INT_MAX ? -1 : sp[a][b]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  cin >> x >> y;
  x--, y--;
  cin >> a >> b;
  a--, b--;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  cout << dijsktra(x, y) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MAX 1000
using namespace std;
int n, m;
string v[MAX + 1];
int visited[MAX + 1][MAX + 1];
vector<int> graph[MAX + 1];
int vis[MAX * MAX + 10];
int movex[] = {-1, +1, 0, 0};
int movey[] = {0, 0, -1, +1};
int a, b;
bool valid(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && v[x][y] == '.');
}
void dfs(int x, int y, int id) {
  visited[x][y] = id;
  for (int i = 0; i < 4; ++i)
    if (valid(x + movex[i], y + movey[i]) &&
        visited[x + movex[i]][y + movey[i]] == 0)
      dfs(x + movex[i], y + movey[i], id);
}
int dfs2(int x) {
  if (x == visited[a][b])
    return 0;
  vis[x] = INT_MAX;
  int ans = INT_MAX;
  for (int y : graph[x]) {
    if (vis[y] == -1) {
      int tmp = dfs2(y);
      if (tmp != INT_MAX)
        ans = min(ans, tmp + 1);
    }
  }
  vis[x] = ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  cin >> x >> y;
  x--, y--;
  cin >> a >> b;
  a--, b--;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  int id = 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (v[i][j] == '.' && visited[i][j] == 0)
        vis[id] = -1, dfs(i, j, id++);
  if (visited[x][y] == visited[a][b])
    cout << "0" << endl;
  else {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (v[i][j] == '.') {
          for (int k = i - 2; k <= i + 2; ++k) {
            for (int l = j - 2; l <= j + 2; ++l) {
              if (valid(k, l) && visited[k][l] != visited[i][j]) {
                graph[visited[k][l]].push_back(visited[i][j]);
                graph[visited[i][j]].push_back(visited[k][l]);
              }
            }
          }
        }
    int ans = dfs2(visited[x][y]);
    if (ans == INT_MAX)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}