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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
  int gy = y[num + 1], gx = x[num + 1];
  int d[1001][1001];
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1001; j++) {
      d[i][j] = INF;
    }
  }
  queue<int> qx;
  queue<int> qy;
  qx.push(x[num]);
  qy.push(y[num]);
  d[y[num]][x[num]] = 0;
  while (qx.size() + qy.size() != 0) {
    int ny, nx;
    ny = qy.front();
    qy.pop();
    nx = qx.front();
    qx.pop();
    if (ny == gy && nx == gx) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int mx = nx + d1[i], my = ny + d2[i];
      if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
          d[my][mx] == INF) {
        d[my][mx] = d[ny][nx] + 1;
        qx.push(mx);
        qy.push(my);
      }
    }
  }
  return d[gy][gx];
}
int main() {
  int ans = 0;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        y[0] = i;
        x[0] = j;
      } else if ('0' < map[i][j] && map[i][j] <= '9') {
        y[(int)(map[i][j] - '0')] = i;
        x[(int)(map[i][j] - '0')] = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    ans += bfs(i);
  }

  cout << ans << endl;
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
  int gy = y[num + 1], gx = x[num + 1];
  int d[1001][1001];
  for (int i = 0; i <= 1001; i++) {
    for (int j = 0; j <= 1001; j++) {
      d[i][j] = INF;
    }
  }
  queue<int> qx;
  queue<int> qy;
  qx.push(x[num]);
  qy.push(y[num]);
  d[y[num]][x[num]] = 0;
  while (qx.size() + qy.size() != 0) {
    int ny, nx;
    ny = qy.front();
    qy.pop();
    nx = qx.front();
    qx.pop();
    if (ny == gy && nx == gx) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int mx = nx + d1[i], my = ny + d2[i];
      if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
          d[my][mx] == INF) {
        d[my][mx] = d[ny][nx] + 1;
        qx.push(mx);
        qy.push(my);
      }
    }
  }
  return d[gy][gx];
}
int main() {
  int ans = 0;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        y[0] = i;
        x[0] = j;
      } else if ('0' < map[i][j] && map[i][j] <= '9') {
        y[(int)(map[i][j] - '0')] = i;
        x[(int)(map[i][j] - '0')] = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    ans += bfs(i);
  }

  cout << ans << endl;
  return 0;
}