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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define ll long long
using namespace std;
const int N = 1111;
char s[N];
int map[N][N], vis[N][N];
int n, m, tot, ax, ay, bx, by, head, tail;
int c[N * N * 10][2], Next[N * N * 10][2];
void bfs() {
  while (head <= tail) {
    int tx = c[head][0], ty = c[head++][1];
    Next[++tot][0] = tx, Next[tot][1] = ty;
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        if (abs(dx) + abs(dy) != 1)
          continue;
        int p = tx + dx, q = ty + dy;
        if (p <= 0 || p > n || q <= 0 || q > m || map[p][q])
          continue;
        if (!vis[p][q])
          c[++tail][0] = p, c[tail][1] = q, vis[p][q] = 1;
      }
  }
}
void find() {
  head = 1, tail = 0;
  for (int i = 1; i <= tot; i++) {
    int tx = Next[i][0], ty = Next[i][1];
    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        int p = tx + dx, q = ty + dy;
        if (p <= 0 || p > n || q <= 0 || q > m || map[p][q])
          continue;
        if (!vis[p][q])
          c[++tail][0] = p, c[tail][1] = q, vis[p][q] = 1;
      }
  }
  tot = 0;
}
void solve() {
  head = 1;
  c[++tail][0] = ax, c[tail][1] = ay;
  int ans = 0;
  while (1) {
    bfs();
    if (vis[bx][by]) {
      printf("%d\\n", ans);
      return;
    }
    if (tot == 0)
      break;
    ans++;
    find();
  }
  printf("-1\\n");
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &ax, &ay, &bx, &by);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      if (s[j] == '#')
        map[i][j] = 1;
      if (s[j] == '.')
        map[i][j] = 0;
    }
  }
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define ll long long
using namespace std;
const int N = 1111;
char s[N];
int map[N][N], Next[N][N], vis[N][N];
int n, m, tot, ax, ay, bx, by, head, tail;
int c[N * N][2];
void bfs() {
  while (head <= tail) {
    int tx = c[head][0], ty = c[head++][1];
    Next[++tot][0] = tx, Next[tot][1] = ty;
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        if (abs(dx) + abs(dy) != 1)
          continue;
        int p = tx + dx, q = ty + dy;
        if (p <= 0 || p > n || q <= 0 || q > m || map[p][q])
          continue;
        if (!vis[p][q])
          c[++tail][0] = p, c[tail][1] = q, vis[p][q] = 1;
      }
  }
}
void find() {
  head = 1, tail = 0;
  for (int i = 1; i <= tot; i++) {
    int tx = Next[i][0], ty = Next[i][1];
    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        int p = tx + dx, q = ty + dy;
        if (p <= 0 || p > n || q <= 0 || q > m || map[p][q])
          continue;
        if (!vis[p][q])
          c[++tail][0] = p, c[tail][1] = q, vis[p][q] = 1;
      }
  }
  tot = 0;
}
void solve() {
  head = 1;
  c[++tail][0] = ax, c[tail][1] = ay;
  int ans = 0;
  while (1) {
    bfs();
    if (vis[bx][by]) {
      printf("%d\\n", ans);
      return;
    }
    if (tot == 0)
      break;
    ans++;
    find();
  }
  printf("-1\\n");
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &ax, &ay, &bx, &by);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      if (s[j] == '#')
        map[i][j] = 1;
      if (s[j] == '.')
        map[i][j] = 0;
    }
  }
  solve();
  return 0;
}