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

#include <bits/stdc++.h>
using namespace std;
int a, b, sx, sy, ex, ey, color[1050][1050], dis[1000050];
int direx[4] = {1, 0, -1, 0};
int direy[4] = {0, 1, 0, -1};
string str[1050];
void bfs(int x, int y) {
  queue<int> Q;
  Q.push(x);
  Q.push(y);
  while (!Q.empty()) {
    int tx = Q.front();
    Q.pop();
    int ty = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int dx = tx + direx[i];
      int dy = ty + direy[i];
      if (dx < 0 || dx >= a || dy < 0 || dy >= b)
        continue;
      if (str[dx][dy] == '#')
        continue;
      if (color[dx][dy])
        continue;
      color[dx][dy] = color[tx][ty];
      Q.push(dx);
      Q.push(dy);
    }
  }
}
vector<int> V[1000050];
void cal(int now) {
  queue<int> Q;
  Q.push(now);
  while (!Q.empty()) {
    int top = Q.front();
    Q.pop();
    for (int x : V[top]) {
      if (dis[x] != 2e9)
        continue;
      dis[x] = dis[top] + 1;
      Q.push(x);
    }
  }
}
int main() {
  cin >> a >> b >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < a; i++)
    cin >> str[i];
  int now = 1;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (color[i][j] || str[i][j] == '#')
        continue;
      color[i][j] = now++;
      bfs(i, j);
    }
  }
  // for(int i=0; i<a; i++)
  // {
  // 	for(int j=0; j<b; j++)
  // 		cout << color[i][j] << ' ';
  // 	cout << '\\n';
  // }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (str[i][j] == '#')
        continue;
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (k < 0 || k >= a || l < 0 || l >= b)
            continue;
          if (str[k][l] == '#')
            continue;
          if (color[i][j] == color[k][l])
            continue;
          V[color[i][j]].push_back(color[k][l]);
        }
      }
    }
  }
  for (int i = 1; i < now; i++)
    dis[i] = 2e9;
  dis[color[sx][sy]] = 0;
  cal(color[sx][sy]);
  if (dis[color[ex][ey]] == 2e9)
    dis[color[ex][ey]] = -1;
  cout << dis[color[ex][ey]] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a, b, sx, sy, ex, ey, color[1050][1050], dis[1050];
int direx[4] = {1, 0, -1, 0};
int direy[4] = {0, 1, 0, -1};
string str[1050];
void bfs(int x, int y) {
  queue<int> Q;
  Q.push(x);
  Q.push(y);
  while (!Q.empty()) {
    int tx = Q.front();
    Q.pop();
    int ty = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int dx = tx + direx[i];
      int dy = ty + direy[i];
      if (dx < 0 || dx >= a || dy < 0 || dy >= b)
        continue;
      if (str[dx][dy] == '#')
        continue;
      if (color[dx][dy])
        continue;
      color[dx][dy] = color[tx][ty];
      Q.push(dx);
      Q.push(dy);
    }
  }
}
vector<int> V[1050];
void dfs(int now) {
  for (int x : V[now]) {
    if (dis[x] < dis[now] + 1)
      continue;
    dis[x] = dis[now] + 1;
    dfs(x);
  }
}
int main() {
  cin >> a >> b >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < a; i++)
    cin >> str[i];
  int now = 1;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (color[i][j] || str[i][j] == '#')
        continue;
      color[i][j] = now++;
      bfs(i, j);
    }
  }
  // for(int i=0; i<a; i++)
  // {
  // 	for(int j=0; j<b; j++)
  // 		cout << color[i][j] << ' ';
  // 	cout << '\\n';
  // }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (str[i][j] == '#')
        continue;
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (k < 0 || k >= a || l < 0 || l >= b)
            continue;
          if (str[k][l] == '#')
            continue;
          if (color[i][j] == color[k][l])
            continue;
          V[color[i][j]].push_back(color[k][l]);
        }
      }
    }
  }
  for (int i = 1; i < now; i++)
    dis[i] = 2e9;
  dis[color[sx][sy]] = 0;
  dfs(color[sx][sy]);
  if (dis[color[ex][ey]] == 2e9)
    dis[color[ex][ey]] = -1;
  cout << dis[color[ex][ey]] << '\\n';
}