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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
vector<pair<int, int>> x[7], y[7];
int H, W;
char S[1005];
vector<int> dp[7];
int main() {
  while (true) {
    cin >> W >> H;
    for (int i = 0; i < 7; i++)
      x[i].clear();
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < H; i++) {
      scanf("%s", &S);
      for (int j = 0; j < W; j++) {
        if (S[j] == 'S') {
          x[0].push_back(make_pair(i, j));
        }
        if (S[j] >= '1' && S[j] <= '5') {
          x[S[j] - '0'].push_back(make_pair(i, j));
        }
        if (S[j] == 'G') {
          x[6].push_back(make_pair(i, j));
        }
      }
    }
    int minx = 999999999, minid = -1;
    for (int i = 0; i < 5; i++) {
      vector<pair<int, int>> H = x[1];
      x[1].clear();
      x[1].push_back(x[0][0]);
      for (int j = 0; j < 7; j++) {
        dp[j].resize(x[j].size());
        for (int k = 0; k < dp[j].size(); k++)
          dp[j][k] = 999999999;
      }
      dp[1][0] = 0;
      for (int j = 2; j < 7; j++) {
        for (int k = 0; k < x[j - 1].size(); k++) {
          for (int l = 0; l < x[j].size(); l++) {
            int d = abs(x[j - 1][k].first - x[j][l].first) +
                    abs(x[j - 1][k].second - x[j][l].second);
            dp[j][l] = min(dp[j][l], dp[j - 1][k] + d);
          }
        }
      }
      x[1] = H;
      for (int j = 1; j <= 5; j++)
        y[j] = x[j];
      for (int j = 1; j <= 5; j++)
        x[j] = y[j % 5 + 1];
      if (minx > dp[6][0]) {
        minx = dp[6][0];
        minid = i + 1;
      }
    }
    if (minx == 999999999)
      cout << "NA" << endl;
    else
      cout << minid << ' ' << minx << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#pragma warning(disable : 4996)
using namespace std;
int H, W;
int dp[1003][1003][5];
int x[1003][1003];
char G[1111];
int Q[6000000];
short Q2[6000000];
int sx = 0, sy = 0, gx = 0, gy = 0;
inline int searching(int I) {
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      for (int k = 0; k < 5; k++)
        dp[i][j][k] = 1 << 30;
    }
  }
  Q[0] = sx * 10000 + sy;
  Q2[0] = 0;
  int L = 0, R = 1;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  dp[sx][sy][0] = 0;
  while (L < R || dp[gx][gy][4] < 100000) {
    int cx = Q[L] / 10000, cy = Q[L] % 10000, F = Q2[L];
    L++;
    for (int i = 0; i < 4; i++) {
      int fx = cx + dx[i], fy = cy + dy[i];
      if (x[fx][fy] == -1)
        continue;
      int V1 = F;
      if (x[fx][fy] == (F + 1 + I) % 5 + 1)
        V1 += 1;
      if (dp[fx][fy][V1] > dp[cx][cy][F] + 1) {
        dp[fx][fy][V1] = dp[cx][cy][F] + 1;
        Q[R] = fx * 10000 + fy;
        Q2[R] = V1;
        R++;
      }
    }
  }
  return dp[gx][gy][4];
}
int main() {
  while (true) {
    scanf("%d%d", &W, &H);
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < H + 3; i++) {
      for (int j = 0; j < W + 3; j++)
        x[i][j] = -1;
    }
    for (int i = 1; i <= H; i++) {
      scanf("%s", G);
      for (int j = 1; j <= W; j++) {
        if (G[j - 1] > '0' && G[j - 1] < '6')
          x[i][j] = G[j - 1] - '0';
        else {
          x[i][j] = 0;
          if (G[j - 1] == 'S') {
            sx = i;
            sy = j;
          }
          if (G[j - 1] == 'G') {
            gx = i;
            gy = j;
          }
        }
      }
    }
    int minx = 100000000, minid = 0;
    for (int i = 0; i < 5; i++) {
      int p = searching(i);
      if (minx > p) {
        minx = p;
        minid = i + 1;
      }
    }
    if (minx > 100000)
      cout << "NA" << endl;
    else {
      cout << minid << ' ' << minx << endl;
    }
  }
  return 0;
}