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

#include <algorithm>
#include <cstdio>
#include <deque>

const int N = 1e3 + 5;
const int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

char s[N][N];
int n, m;
int stx, sty;
int edx, edy;

struct Point {
  int x, y;
};
std::deque<Point> q;
int dis[N][N];
void bfs() {
  q.push_back({stx, sty});
  dis[stx][sty] = 0;
  while (!q.empty()) {
    //		std::printf("--------------------------------\\nq = { ");
    //		for(const Point &p : q) std::printf("(%d, %d) ", p.x, p.y);
    //		std::printf("}\\n");
    //		for(int i = 1; i <= n; i++)
    //			for(int j = 1; j <= m; j++)
    //				std::printf("%d%c", dis[i][j], j == m ? '\\n' : '
    //');
    int x = q.front().x, y = q.front().y;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int dx = x + d[i][0], dy = y + d[i][1];
      if (1 <= dx && dx <= n && 1 <= dy && dy <= m &&
          (dis[dx][dy] == -1 || dis[dx][dy] > dis[x][y]) && s[dx][dy] == '.') {
        dis[dx][dy] = dis[x][y];
        q.push_front({dx, dy});
      }
    }
    for (int i = x - 2; i <= x + 2; i++)
      for (int j = y - 2; j <= y + 2; j++)
        if (1 <= i && i <= n && 1 <= j && j <= m &&
            (dis[i][j] == -1 || dis[i][j] > dis[x][y]) && s[i][j] == '.') {
          dis[i][j] = dis[x][y] + 1;
          q.push_back({i, j});
        }
  }
}

int main() {
  std::scanf("%d%d%d%d%d%d", &n, &m, &stx, &sty, &edx, &edy);
  for (int i = 1; i <= n; i++)
    std::scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      dis[i][j] = -1;
  bfs();
  std::printf("%d\\n", dis[edx][edy]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

const int N = 1e3 + 5;

char s[N][N];
int n, m;
int stx, sty;
int edx, edy;

struct Edge {
  int to, nxt;
} edge[N * N];
int head[N];
void add_edge(int u, int v) {
  static int k = 1;
  edge[k] = (Edge){v, head[u]};
  head[u] = k++;
}

int belong[N][N];
bool vis[N][N];
void dfs(int x, int y, int id) {
  static const int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  for (int i = 0; i < 4; i++) {
    int dx = x + dir[i][0], dy = y + dir[i][1];
    if (1 <= dx && dx <= n && 1 <= dy && dy <= m && !vis[dx][dy] &&
        s[dx][dy] == '.') {
      vis[dx][dy] = true;
      belong[dx][dy] = id;
      dfs(dx, dy, id);
    }
  }
}

int dis[N * N];
void bfs(int stdot) {
  static int q[N * N];
  int st = 0, ed = 0;
  q[ed++] = stdot;
  dis[stdot] = 0;
  while (st < ed) {
    int u = q[st++];
    for (int i = head[u]; i; i = edge[i].nxt)
      if (edge[i].to != stdot && !dis[edge[i].to]) {
        dis[edge[i].to] = dis[u] + 1;
        q[ed++] = edge[i].to;
      }
  }
}

int main() {
  std::scanf("%d%d", &n, &m);
  std::scanf("%d%d%d%d", &stx, &sty, &edx, &edy);
  for (int i = 1; i <= n; i++)
    std::scanf("%s", s[i] + 1);
  int id = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!vis[i][j])
        dfs(i, j, ++id);
  //	for(int i = 1; i <= n; i++)
  //		for(int j = 1; j <= m; j++)
  //			std::printf("belong[%d][%d] = %d\\n", i, j,
  //belong[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == '.')
        for (int r = std::max(1, i - 2); r <= std::min(n, i + 2); r++)
          for (int c = std::max(1, j - 2); c <= std::min(m, j + 2); c++)
            if (s[r][c] == '.' && belong[i][j] != belong[r][c]) {
              add_edge(belong[i][j], belong[r][c]);
              add_edge(belong[r][c], belong[i][j]);
            }
  bfs(belong[stx][sty]);
  std::printf("%d\\n", dis[belong[edx][edy]]);
  return 0;
}