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

#include <iostream>
#include <queue>
using namespace std;
int H, W, N, a, b, c, d, nh, nw;
int cost[1000][1000];
char t[1000][1000];
queue<int> qh;
queue<int> qw;
int th[4] = {0, -1, 0, 1};
int tw[4] = {1, 0, -1, 0};
int map[10][2];
int bfs(int tah, int taw) {
  int h, w, ans;
  while (!qw.empty()) {
    h = qh.front(), qh.pop();
    w = qw.front(), qw.pop();
    for (int i = 0; i < 4; i++) {
      nh = h + th[i];
      nw = w + tw[i];
      if (0 <= nh && nh < H && 0 <= nw && nw < W) {
        if (nh == tah && nw == taw) {
          ans = cost[h][w] + 1;
          return ans;
        } else if (cost[nh][nw] == -1) {
          qh.push(nh);
          qw.push(nw);
          cost[nh][nw] = cost[h][w] + 1;
        }
      }
    }
  }
}
void syokika() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (cost[i][j] != -2)
        cost[i][j] = -1;
    }
  }
  while (!qh.empty()) {
    qh.pop();
    qw.pop();
  }
}
int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> t[i][j];
      if (t[i][j] == 'S') {
        map[0][0] = i;
        map[0][1] = j;
      } else if (t[i][j] == 'X') {
        cost[i][j] = -2;
      } else if (t[i][j] != '.') {
        map[t[i][j] - '0'][0] = i;
        map[t[i][j] - '0'][1] = j;
      } else {
      }
    }
  }
  int sum = 0, a;
  for (int i = 0; i < N; i++) {
    syokika();

    qh.push(map[i][0]);
    qw.push(map[i][1]);
    cost[map[i][0]][map[i][1]] = 0;
    a = bfs(map[i + 1][0], map[i + 1][1]);
    sum += a;
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int H, W, N, a, b, c, d, nh, nw;
int cost[1000][1000];
char t[1000][1000];
queue<int> qh;
queue<int> qw;
int th[4] = {0, -1, 0, 1};
int tw[4] = {1, 0, -1, 0};
int map[10][2];
int bfs(int tah, int taw) {
  int h, w, ans;
  while (!qw.empty()) {
    h = qh.front(), qh.pop();
    w = qw.front(), qw.pop();
    for (int i = 0; i < 4; i++) {
      nh = h + th[i];
      nw = w + tw[i];
      if (nh == tah && nw == taw) {
        ans = cost[h][w] + 1;
        return ans;
      } else if (cost[nh][nw] == -1) {
        qh.push(nh);
        qw.push(nw);
        cost[nh][nw] = cost[h][w] + 1;
      }
    }
  }
}
void syokika() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (cost[i][j] != -2)
        cost[i][j] = -1;
    }
  }
  while (!qh.empty()) {
    b = qh.front(), qh.pop();
    b = qw.front(), qw.pop();
  }
}
int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> t[i][j];
      if (t[i][j] == 'S') {
        map[0][0] = i;
        map[0][1] = j;
      } else if (t[i][j] == 'X') {
        cost[i][j] = -2;
      } else if (t[i][j] != '.') {
        map[t[i][j] - '0'][0] = i;
        map[t[i][j] - '0'][1] = j;
      } else {
      }
    }
  }
  int sum = 0, a;
  for (int i = 0; i < N; i++) {
    syokika();

    qh.push(map[i][0]);
    qw.push(map[i][1]);
    cost[map[i][0]][map[i][1]] = 0;
    a = bfs(map[i + 1][0], map[i + 1][1]);
    sum += a;
  }
  cout << sum << endl;
  return 0;
}