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

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define lln long long int
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

vector<vector<int>> dis(1001, vector<int>(1001, -1));
vector<string> grid;
int n, m, x1, yy1, x2, y2;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};

void bfs() {
  cin >> n >> m >> x1 >> yy1 >> x2 >> y2;
  x1--;
  yy1--;
  x2--;
  y2--;
  grid = vector<string>(n);
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  dis[x1][yy1] = 0;
  deque<pair<int, int>> q;
  q.push_back({x1, yy1});
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = x - dx[i];
      int ny = y - dy[i];
      if (nx < 0 || nx >= n)
        continue;
      if (ny < 0 || ny >= m)
        continue;
      if (grid[nx][ny] == '#')
        continue;
      if (dis[nx][ny] != -1 && dis[nx][ny] <= dis[x][y])
        continue;
      dis[nx][ny] = dis[x][y];
      q.push_front({nx, ny});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= n)
          continue;
        if (ny < 0 || ny >= m)
          continue;
        if (grid[nx][ny] == '#' || dis[nx][ny] != -1)
          continue;
        dis[nx][ny] = dis[x][y] + 1;
        q.push_back({nx, ny});
      }
    }
  }
}

int main() {
  IOS;

  bfs();
  cout << dis[x2][y2] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define lln long long int
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

int n, m, x1, yy1, x2, y2;
const int INF = 1e9;
int s, d;
int visited[1001][1001];
int visited1[1001][1001];
vector<pair<int, int>> adj;
vector<vector<int>> table;
vector<string> grid;

void bfs(int i, int j, int val) {
  if (i < 0 || i >= n || j < 0 || j >= m || visited[i][j] || grid[i][j] == '#')
    return;
  visited[i][j] = val;
  if (i == x1 && j == yy1)
    s = val;
  if (i == x2 && j == y2)
    d = val;
  bfs(i - 1, j, val);
  bfs(i + 1, j, val);
  bfs(i, j - 1, val);
  bfs(i, j + 1, val);
}

void creategraph(int i, int j, int val) {
  if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == '#' || visited1[i][j])
    return;
  if (val != visited[i][j] && visited[i][j]) {
    if (!table[val][visited[i][j]]) {
      adj.push_back({val, visited[i][j]});
    }
    table[val][visited[i][j]] = 1;
    return;
  }
  visited1[i][j] = 1;
  for (int a = -2; a <= 2; a++) {
    for (int b = -2; b <= 2; b++) {
      creategraph(i + a, j + b, val);
    }
  }
}

int main() {
  IOS;

  cin >> n >> m >> x1 >> yy1 >> x2 >> y2;
  x1--;
  yy1--;
  x2--;
  y2--;
  grid = vector<string>(n);
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  int val = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!visited[i][j] && grid[i][j] == '.') {
        bfs(i, j, val);
        val++;
      }
    }
  }
  val--;
  table = vector<vector<int>>(val + 1, vector<int>(val + 1, 0));
  vector<int> cg(val + 1, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (visited[i][j] && !cg[visited[i][j]]) {
        creategraph(i, j, visited[i][j]);
        cg[visited[i][j]]++;
      }
    }
  }
  vector<int> dist(val + 1, INF);
  dist[s] = 0;
  for (int i = 1; i <= val; i++) {
    for (auto i : adj) {
      dist[i.second] = min(dist[i.second], dist[i.first] + 1);
      dist[i.first] = min(dist[i.first], dist[i.second] + 1);
    }
  }
  cout << (dist[d] == INF ? -1 : dist[d]) << endl;

  return 0;
}