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

#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(x, to) for (int x = 0; x < (to); ++(x))
typedef pair<int, int> pi;
int memo[1000][5];
int w, h;
string str;
int dist(pi &a, pi &b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int main() {
  while (cin >> w >> h && (w || h)) {
    pi s, g;
    vector<pi> f[5];
    rep(i, h) {
      cin >> str;
      rep(j, w) {
        if (str[j] == 'S')
          s = make_pair(i, j);
        else if (str[j] == 'G')
          g = make_pair(i, j);
        else if (str[j] == '1')
          f[str[j] - '1'].push_back(make_pair(i, j));
        else if (str[j] == '2')
          f[str[j] - '1'].push_back(make_pair(i, j));
        else if (str[j] == '3')
          f[str[j] - '1'].push_back(make_pair(i, j));
        else if (str[j] == '4')
          f[str[j] - '1'].push_back(make_pair(i, j));
        else if (str[j] == '5')
          f[str[j] - '1'].push_back(make_pair(i, j));
      }
    }
    int ans = 1 << 30, ret = -1;
    rep(i, 5) {
      int cur = i, next = (i + 1) % 5;
      rep(j, f[next].size()) { memo[j][next] = dist(s, f[next][j]); }
      cur = next, next = (next + 1) % 5;
      rep(j, 3) {
        rep(k, f[next].size()) {
          memo[k][next] = 1 << 30;
          rep(l, f[cur].size()) {
            memo[k][next] =
                min(memo[k][next], memo[l][cur] + dist(f[cur][l], f[next][k]));
          }
        }
        cur = next, next = (next + 1) % 5;
      }
      rep(j, f[cur].size()) {
        int tmp = memo[j][cur] + dist(f[cur][j], g);
        if (tmp < ans) {
          ans = tmp, ret = i;
        }
      }
    }
    if (ans == 1 << 30)
      cout << "NA" << endl;
    else
      cout << ret + 1 << " " << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#define rep2(x, from, to) for (int x = (from); (x) < (to); (x)++)
#define rep(x, to) rep2(x, 0, to)
using namespace std;
int b[1010][1010] = {{}};
bool v[1010][1010] = {{}};
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int x, y, sx, sy, gx, gy;
string st;
const int INF = 99999999;
struct S {
  int x, y, t;
};
int bfs(int a) {
  int c = a;
  queue<S> q;
  S s = {sx, sy, 0};
  q.push(s);
  bool f = 1;
  while (!q.empty()) {
    S cs = q.front();
    v[cs.y][cs.x] = 1;
    if (!f && cs.x == gx && cs.y == gy)
      break;
    q.pop();
    if (f && b[cs.y][cs.x] == (c + 1) % 5) {
      if (c == (a + 5 - 2) % 5)
        f = 0;
      c = (c + 1) % 5;
      rep(i, 1010) {
        rep(j, 1010) { v[i][j] = 0; }
      }
      queue<S> nq;
      nq.push(cs);
      q = nq;
      continue;
    }
    rep(i, 4) {
      int ax = cs.x + dx[i], ay = cs.y + dy[i];
      if (b[ay][ax] == -2 || v[ay][ax])
        continue;
      S ns = {ax, ay, cs.t + 1};
      q.push(ns);
    }
  }
  if (q.empty())
    return INF;
  else
    return q.front().t;
}
int main() {
  while (cin >> x >> y, (x || y)) {
    rep(i, 1010) {
      rep(j, 1010) { b[i][j] = -1; }
    }
    rep(i, y + 2) {
      b[i][0] = -2;
      b[i][x + 1] = -2;
    }
    rep(i, x + 2) {
      b[0][i] = -2;
      b[y + 1][i] = -2;
    }
    rep(i, y) {
      cin >> st;
      rep(j, x) {
        if (st[j] == '.')
          continue;
        else if (st[j] == 'S')
          sx = j + 1, sy = i + 1;
        else if (st[j] == 'G')
          gx = j + 1, gy = i + 1;
        else
          b[i + 1][j + 1] = (st[j] - '0') - 1;
      }
    }
    int ret = INF, ans = -1;
    rep(i, 5) {
      rep(j, 1010) {
        rep(k, 1010) { v[j][k] = 0; }
      }
      int x = bfs(i);
      if (x < ret)
        ret = x, ans = i;
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans + 1 << " " << ret << endl;
  }
}