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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1010;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct node {
  int x, y;
  int dep;
};
node q[maxn * maxn];
int a[maxn][maxn], vis[maxn][maxn];
int x[10], y[10];
int h, w, n;
int bfs(int s, int t) {
  memcpy(vis, a, sizeof(a)); // vis=a;
  q[0] = (node){x[s], y[s], 0};
  vis[x[s]][y[s]] = 1;
  int head = 0, tail = 1;
  while (head < tail) {
    node cur = q[head++];
    if (cur.x == x[t] && cur.y == y[t])
      return cur.dep;
    for (int i = 0; i < 4; i++) {
      int xx = cur.x + dx[i], yy = cur.y + dy[i];
      if (xx >= 0 && xx < h && yy >= 0 && yy < w && !vis[xx][yy]) {
        q[tail++] = (node){xx, yy, cur.dep + 1};
        vis[xx][yy] = 1;
      }
    }
  }
}
int main() {
  cin >> h >> w >> n;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      char ch;
      cin >> ch;
      if (ch == 'S') {
        x[0] = i;
        y[0] = j;
      }
      if (ch == 'X')
        a[i][j] = 1;
      if (ch >= '1' && ch <= '9') {
        x[ch - 48] = i;
        y[ch - 48] = j;
      }
    }
  int ans = 0, s;
  for (int i = 0; i < n; i++)
    ans += bfs(i, i + 1);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int w, h, n;
char map[1024][1024];
int d[1024][1024];
const int direction[4][2] = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1},
};
int factory[16][2];
typedef pair<int, int> P;
/*int bfs(int sx, int sy, int gx, int gy) {
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            d[i][j] = -1;
        }
    }
    queue<P> que;
    que.push(P(sx, sy));
    d[sx][sy] = 0;
    while (que.size()) {
        P p = que.front();
        que.pop();
        if (p.first == gx && p.second == gy) {
            break;
        }
        for (int i = 0; i < 4; ++i) {
            int nx = p.first + direction[i][0];
            int ny = p.second + direction[i][1];
            if (0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] != 'X' &&
d[nx][ny] == -1) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }

    return d[gx][gy];
}*/
int bfs(int sx, int sy, int ex, int ey) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      d[i][j] = -1;
  }
  queue<P> q;
  q.push(P(sx, sy));
  d[sx][sy] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.first == ex && p.second == ey)
      break;
    for (int i = 0; i < 4; i++) {
      int dx = p.first + direction[i][0];
      int dy = p.second + direction[i][1];
      if (dx >= 0 && dy >= 0 && dx < h && dy < w && map[dx][dy] != 'X' &&
          d[dx][dy] == -1)
        q.push(P(dx, dy));
      d[dx][dy] = d[p.first][p.second] + 1;
    }
  }
  return d[ex][ey];
}
/*int main() {

    cin >> h >> w >> n;
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> map[i][j];
        }
    }

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (map[i][j] == 'S') {
                factory[0][0] = i;
                factory[0][1] = j;
                map[i][j] = '.';
            } else if (isdigit(map[i][j])) {
                int index = map[i][j] - '0';
                factory[index][0] = i;
                factory[index][1] = j;
            }
        }
    }

    int step = 0;
    for (int i = 0; i < n; ++i) {
        step += bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i +
1][1]);
    }

    cout << step << endl;

    return 0;
}*/
int main() {
  scanf("%d %d %d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", map[i]);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 'S') {
        factory[0][0] = i;
        factory[0][1] = j;
        map[i][j] = '.';
      } else if (isdigit(map[i][j])) {
        factory[map[i][j] - '0'][0] = i;
        factory[map[i][j] - '0'][1] = j;
        map[i][j] = '.';
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans +=
        bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
  printf("%d\\n", ans);
  return 0;
}
///////////////////////////End Sub//////////////////////////////////