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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int N = 1e3 + 5;
char s[N][N];
vector<int> g[N * N];
bool vis[N * N];
int h, w, d[N * N];
int color[N][N], cp;
int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
void dfs(int x, int y) {
  color[x][y] = cp;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 1 || nx > h || ny < 1 || ny > w)
      continue;
    if (!color[nx][ny] && s[nx][ny] != '#')
      dfs(nx, ny);
  }
}
queue<int> qi;
int main() {
  int ch, cw, dh, dw;
  scanf("%d %d %d %d %d %d", &h, &w, &ch, &cw, &dh, &dw);
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (s[i][j] == '.' && !color[i][j]) {
        ++cp;
        dfs(i, j);
      }
    }
  }
  for (int tx = 1; tx <= h; ++tx) {
    for (int ty = 1; ty <= w; ++ty) {
      if (s[tx][ty] == '#')
        continue;
      for (int j = -2; j < 3; ++j) {
        for (int k = -2; k < 3; ++k) {
          int nx = tx + j, ny = ty + k;
          if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#' ||
              (nx == tx && ny == ty))
            continue;
          if (color[tx][ty] != color[nx][ny]) {
            g[color[tx][ty]].push_back(color[nx][ny]);
            g[color[nx][ny]].push_back(color[tx][ty]);
          }
        }
      }
    }
  }
  memset(d, 0x3f, sizeof(d));
  d[color[ch][cw]] = 0;
  vis[color[ch][cw]] = true;
  qi.push(color[ch][cw]);
  while (!qi.empty()) {
    int u = qi.front();
    qi.pop();
    for (int v : g[u]) {
      if (d[v] > d[u] + 1) {
        d[v] = d[u] + 1;
        if (!vis[v]) {
          vis[v] = true;
          qi.push(v);
        }
      }
    }
  }
  if (d[color[dh][dw]] < 0x3f3f3f3f)
    printf("%d\\n", d[color[dh][dw]]);
  else
    puts("-1");
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int N = 1e3 + 5;
char s[N][N];
vector<int> g[N * N];
bool vis[N];
int h, w, d[N];
int color[N][N], cp;
int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
void dfs(int x, int y) {
  color[x][y] = cp;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 1 || nx > h || ny < 1 || ny > w)
      continue;
    if (!color[nx][ny] && s[nx][ny] != '#')
      dfs(nx, ny);
  }
}
queue<int> qi;
int main() {
  int ch, cw, dh, dw;
  scanf("%d %d %d %d %d %d", &h, &w, &ch, &cw, &dh, &dw);
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (s[i][j] == '.' && !color[i][j]) {
        ++cp;
        dfs(i, j);
      }
    }
  }
  for (int i = 0; i < 1000000; ++i) {
    int x = rand() % h + 1, y = rand() % w + 1;
    if (s[x][y] == '#')
      continue;
    for (int j = 0; j < 5; ++j) {
      int nx = x + rand() % 5 - 2, ny = y + rand() % 5 - 2;
      if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#' ||
          (nx == x && ny == y))
        continue;
      if (color[x][y] != color[nx][ny]) {
        g[color[x][y]].push_back(color[nx][ny]);
        g[color[nx][ny]].push_back(color[x][y]);
      }
    }
  }
  memset(d, 0x3f, sizeof(d));
  d[color[ch][cw]] = 0;
  vis[color[ch][cw]] = true;
  qi.push(color[ch][cw]);
  while (!qi.empty()) {
    int u = qi.front();
    qi.pop();
    for (int v : g[u]) {
      if (d[v] > d[u] + 1) {
        d[v] = d[u] + 1;
        if (!vis[v]) {
          vis[v] = true;
          qi.push(v);
        }
      }
    }
  }
  if (d[color[dh][dw]] < 0x3f3f3f3f)
    printf("%d\\n", d[color[dh][dw]]);
  else
    puts("-1");
  return 0;
}