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

#include <cctype>
#include <iostream>
#include <queue>

using namespace std;

int cnt;
char field[1002][1002];
int cost[1002][1002];
int factory[9][2];
int x[4] = {1, 0, -1, 0};
int y[4] = {0, 1, 0, -1};

int bfs(int sx, int sy, int gx, int gy) {
  queue<pair<int, int>> que;

  for (int i = 0; i < 1002; i++) {
    for (int j = 0; j < 1002; j++) {
      cost[i][j] = -1;
    }
  }

  que.push(make_pair(sx, sy));
  cost[sx][sy] = 0;

  while (!que.empty()) {
    pair<int, int> p;
    p = que.front();
    que.pop();

    if (p.first == gx && p.second == gy) {
      break;
    }

    for (int i = 0; i < 4; i++) {
      int tx = p.first + x[i];
      int ty = p.second + y[i];
      if (field[tx][ty] != 'X' && cost[tx][ty] == -1) {
        que.push(pair<int, int>(tx, ty));
        cost[tx][ty] = cost[p.first][p.second] + 1;
      }
    }
  }
  return cost[gx][gy];
}

int main() {
  int w, h, n, sx, sy;

  cin >> h >> w >> n;

  for (int y = 1; y <= h; y++) {
    for (int x = 1; x <= w; x++) {
      cin >> field[x][y];
      if (field[x][y] == 'S') {
        sx = x;
        sy = y;
      } else if (isdigit(field[x][y])) {
        int tmp = field[x][y] - '0';
        factory[tmp][0] = x;
        factory[tmp][1] = y;
      }
    }
    field[0][y] = 'X';
    field[w + 1][y] = 'X';
  }

  for (int x = 0; x <= w + 1; x++) {
    field[x][0] = 'X';
    field[x][h + 1] = 'X';
  }

  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    // 次の目的地へ行くで
    cnt += bfs(sx, sy, factory[i][0], factory[i][1]);
    sx = factory[i][0];
    sy = factory[i][1];
  }

  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
#define PB push_back
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int r, c, n;
vector<P> cheese;

struct G {
  int grid[1000][1000];

public:
  void init() {
    REP(i, 1000) {
      REP(j, 1000) { grid[i][j] = -1; }
    }
    cin >> r >> c >> n;
    cheese.resize(n + 1);
    string line;
    REP(i, r) {
      cin >> line;
      REP(j, c) {
        switch (line[j]) {
        case 'X':
          grid[i + 1][j + 1] = -1;
          break;
        case '.':
          grid[i + 1][j + 1] = INF;
          break;
        case 'S':
          grid[i + 1][j + 1] = INF;
          cheese[0].first = i + 1;
          cheese[0].second = j + 1;
          break;
        default:
          int t = (int)line[j] - '0';
          cheese[t].first = i + 1;
          cheese[t].second = j + 1;
          grid[i + 1][j + 1] = INF;
        }
      }
    }
  }
};

int bfs(int sr, int sc, int gr, int gc, struct G g) {
  queue<P> q;
  q.push(MP(sr, sc));
  g.grid[sr][sc] = 0;
  int nr, nc;
  while (!q.empty()) {
    nr = q.front().first;
    nc = q.front().second;
    q.pop();
    if (nr == gr && nc == gc)
      return g.grid[nr][nc];
    REP(i, 4) {
      if (g.grid[nr + dy[i]][nc + dx[i]] == INF) {
        g.grid[nr + dy[i]][nc + dx[i]] = g.grid[nr][nc] + 1;
        q.push(MP(nr + dy[i], nc + dx[i]));
      }
    }
  }
  return 0;
}

int main() {
  struct G g;
  g.init();
  int ans = 0;
  REP(i, n) {
    ans += bfs(cheese[i].first, cheese[i].second, cheese[i + 1].first,
               cheese[i + 1].second, g);
  }
  cout << ans << endl;
  return 0;
}