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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int maxn = 1111;
int d[maxn][maxn];
bool was[maxn][maxn];
char a[maxn][maxn];
vector<int> g[maxn * maxn];

void dfs(int x, int y, int c) {
  if (was[x][y])
    return;
  if (a[x][y] == '#')
    return;
  d[x][y] = c;
  was[x][y] = 1;
  dfs(x + 1, y, c);
  dfs(x - 1, y, c);
  dfs(x, y + 1, c);
  dfs(x, y - 1, c);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++) {
      a[i][j] = '#';
    }
  }
  int n, m;
  cin >> n >> m;
  int c1, c2, d1, d2;
  cin >> c1 >> c2 >> d1 >> d2;
  c1++, c2++, d1++, d2++;
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      cin >> a[i][j];
    }
  }
  if (a[c1][c2] == '#' or a[d1][d2] == '#') {
    cout << -1 << endl;
    return 0;
  }
  int col = 0;
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      if (!was[i][j] and a[i][j] != '#')
        dfs(i, j, ++col);
    }
  }
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (a[i][j] == '.' and a[k][l] == '.' and d[i][j] != d[k][l]) {
            g[d[i][j]].push_back(d[k][l]);
            g[d[k][l]].push_back(d[i][j]);
          }
        }
      }
    }
  }
  queue<int> q;
  q.push(d[c1][c2]);
  int dist[col + 1];
  fill(dist, dist + col + 1, n * m + 1);
  dist[d[c1][c2]] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i : g[x]) {
      if (dist[i] > dist[x] + 1) {
        dist[i] = dist[x] + 1;
        q.push(i);
      }
    }
  }
  if (dist[d[d1][d2]] == n * m + 1)
    cout << -1 << endl;
  else
    cout << dist[d[d1][d2]] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int maxn = 1111;
int d[maxn][maxn];
bool was[maxn][maxn];
char a[maxn][maxn];
bool adj[maxn][maxn];

void dfs(int x, int y, int c) {
  if (was[x][y])
    return;
  if (a[x][y] == '#')
    return;
  d[x][y] = c;
  was[x][y] = 1;
  dfs(x + 1, y, c);
  dfs(x - 1, y, c);
  dfs(x, y + 1, c);
  dfs(x, y - 1, c);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < maxn; i++) {
    for (int j = 0; j < maxn; j++) {
      a[i][j] = '#';
    }
  }
  int n, m;
  cin >> n >> m;
  int c1, c2, d1, d2;
  cin >> c1 >> c2 >> d1 >> d2;
  c1++, c2++, d1++, d2++;
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      cin >> a[i][j];
    }
  }
  if (a[c1][c2] == '#' or a[d1][d2] == '#') {
    cout << -1 << endl;
    return 0;
  }
  int col = 0;
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      if (!was[i][j] and a[i][j] != '#')
        dfs(i, j, ++col);
    }
  }
  for (int i = 2; i <= n + 1; i++) {
    for (int j = 2; j <= m + 1; j++) {
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (a[i][j] == '.' and a[k][l] == '.' and d[i][j] != d[k][l]) {
            adj[d[i][j]][d[k][l]] = adj[d[k][l]][d[i][j]] = 1;
          }
        }
      }
    }
  }
  queue<int> q;
  q.push(d[c1][c2]);
  int dist[col + 1];
  fill(dist, dist + col + 1, n * m + 1);
  dist[d[c1][c2]] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 1; i <= col; i++) {
      if (adj[x][i] and dist[i] > dist[x] + 1) {
        dist[i] = dist[x] + 1;
        q.push(i);
      }
    }
  }
  if (dist[d[d1][d2]] == n * m + 1)
    cout << -1 << endl;
  else
    cout << dist[d[d1][d2]] << endl;
  return 0;
}