Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> P;
const int INF = 1e+9;
int H, W, N;
int d[1010][1010];
char board[1010][1010];
int sx, sy;
int gx, gy;
int ans;
void bfs(int y, int x, int k) {
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      d[i][j] = INF;
      if (board[i][j] == k + '0') {
        gx = j;
        gy = i;
      }
    }
  }
  d[y][x] = 0;
  queue<P> que;
  que.push(P(y, x));
  while (!que.empty()) {
    P p = que.front();
    que.pop();

    if (p.first == gy && p.second == gx)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.second + dx[i];
      int ny = p.first + dy[i];
      if (nx >= 1 && nx <= W && ny >= 1 && ny <= H && board[ny][nx] != 'X' &&
          d[ny][nx] > d[p.first][p.second]) {
        que.push(P(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  ans += d[gy][gx];
  if (k != N)
    bfs(gy, gx, k + 1);
}

int main() {
  cin >> H >> W >> N;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> board[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (board[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  bfs(sy, sx, 1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
  std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> P;
const int INF = 1e+9;
int H, W, N;
int d[1010][1010];
char board[1010][1010];
int sx, sy;
int gx, gy;
int ans;
void bfs(int y, int x, int k) {
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      d[i][j] = INF;
      if (board[i][j] == k + '0') {
        gx = j;
        gy = i;
      }
    }
  }
  d[y][x] = 0;
  queue<P> que;
  que.push(P(y, x));
  while (!que.empty()) {
    P p = que.front();
    que.pop();

    if (p.first == gy && p.second == gx)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.second + dx[i];
      int ny = p.first + dy[i];
      if (nx >= 1 && nx <= W && ny >= 1 && ny <= H && board[ny][nx] != 'X' &&
          d[ny][nx] == INF) {
        que.push(P(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  ans += d[gy][gx];
  if (k != N)
    bfs(gy, gx, k + 1);
}

int main() {
  cin >> H >> W >> N;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> board[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (board[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  bfs(sy, sx, 1);
  cout << ans << endl;
  return 0;
}