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

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000001
#define MOD 100000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
struct A {
  int x, y, cost;
};
int main() {
  int h, w, n, fin = -1;
  int x[11], y[11];
  memset(x, -1, sizeof(x));
  memset(y, -1, sizeof(y));
  cin >> h >> w >> n;
  char c;
  bool fld[1000][1000];
  REP(i, h) REP(j, w) {
    cin >> c;
    if (c == '.')
      fld[j][i] = true;
    else if (c == 'X')
      fld[j][i] = false;
    else if (c == 'S') {
      fld[j][i] = true;
      x[0] = j;
      y[0] = i;
    } else {
      fld[j][i] = true;
      x[c - '0'] = j;
      y[c - '0'] = i;
      fin = max(fin, c - '0');
    }
  }
  int ans = 0;
  REP(i, fin) {
    int d[1000][1000];
    memset(d, -1, sizeof(d));
    queue<A> Q;
    Q.push((A){x[i], y[i], 0});
    while (!Q.empty()) {
      A a = Q.front();
      Q.pop();
      int xx = a.x, yy = a.y, cost = a.cost;
      //                  cout<<xx<<" "<<yy<<endl;
      if (d[xx][yy] != -1)
        continue;
      d[xx][yy] = cost;
      //                  cout<<xx<<" "<<yy<<" "<<cost<<endl;
      if (xx == x[i + 1] && yy == y[i + 1]) {
        ans += cost;
        break;
      }
      REP(j, 4) {
        int nx = xx + dx[j], ny = yy + dy[j];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h || !fld[nx][ny])
          continue;
        Q.push((A){nx, ny, cost + 1});
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF 1000000001
#define MOD 100000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
struct A {
  int x, y, cost;
};
int main() {
  int h, w, n, fin = -1;
  int x[11], y[11];
  memset(x, -1, sizeof(x));
  memset(y, -1, sizeof(y));
  cin >> h >> w >> n;
  char c;
  bool fld[1000][1000];
  REP(i, h) REP(j, w) {
    cin >> c;
    if (c == '.')
      fld[j][i] = true;
    else if (c == 'X')
      fld[j][i] = false;
    else if (c == 'S') {
      fld[j][i] = true;
      x[0] = j;
      y[0] = i;
    } else {
      fld[j][i] = true;
      x[c - '0'] = j;
      y[c - '0'] = i;
      fin = max(fin, c - '0');
    }
  }
  int ans = 0;
  REP(i, fin) {
    int d[1000][1000];
    memset(d, -1, sizeof(d));
    queue<A> Q;
    Q.push((A){x[i], y[i], 0});
    while (!Q.empty()) {
      A a = Q.front();
      Q.pop();
      int xx = a.x, yy = a.y, cost = a.cost;
      d[xx][yy] = cost;
      //                  cout<<xx<<" "<<yy<<endl;
      if (xx == x[i + 1] && yy == y[i + 1]) {
        ans += cost;
        break;
      }
      REP(j, 4) {
        int nx = xx + dx[j], ny = yy + dy[j];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h || !fld[nx][ny])
          continue;
        if (d[nx][ny] != -1)
          continue;
        Q.push((A){nx, ny, cost + 1});
      }
    }
  }
  cout << ans << endl;
}