Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>

using namespace std;

const int INF = 100000000;
const int MAX_W = 1000;
const int MAX_H = 1000;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  queue<P> que;
  P start;
  int pow = 1;
  while (std::cin >> H >> W >> N, H) {
    for (int i = 0; i < H; i++) {
      std::cin >> field[i];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
        if (field[i][j] == 'S') {
          start = P(i, j);
        }
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;
    int count = 0;
    while (que.size()) {
      P p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + N and N <= pow) {
        count = d[p.first][p.second];
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if (nx >= 0 and ny >= 0 and nx < W and ny < H and
            field[ny][nx] != 'X' and d[ny][nx] == INF) {
          if (field[ny][nx] == '.') {
            que.push(P(ny, nx));
            d[ny][nx] = d[p.first][p.second] + 1;
          } else {
            if (field[ny][nx] <= '0' + pow) {
              // cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
              int s = d[p.first][p.second];
              field[ny][nx] = '.';
              for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                  d[i][j] = INF;
                }
              }

              while (que.size()) {
                que.pop();
              }
              que.push(P(ny, nx));
              d[ny][nx] = s + 1;
              if (pow == N) {
                field[ny][nx] = '0' + N;
              }
              pow++;
              break;
            } else {
              que.push(P(ny, nx));
              d[ny][nx] = d[p.first][p.second] + 1;
            }
          }
        }
      }
    }

    std::cout << count << std::endl;
    break;
  }

  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>

using namespace std;

const int INF = 100000000;
const int MAX_W = 1001;
const int MAX_H = 1001;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  queue<P> que;
  P start;
  int pow = 1;
  while (std::cin >> H >> W >> N, H) {
    for (int i = 0; i < H; i++) {
      std::cin >> field[i];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
        if (field[i][j] == 'S') {
          start = P(i, j);
        }
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;
    int count = 0;
    while (que.size()) {
      P p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + N and N <= pow) {
        count = d[p.first][p.second];
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if (nx >= 0 and ny >= 0 and nx < W and ny < H and
            field[ny][nx] != 'X' and d[ny][nx] == INF) {
          if (field[ny][nx] == '.') {
            que.push(P(ny, nx));
            d[ny][nx] = d[p.first][p.second] + 1;
          } else {
            if (field[ny][nx] <= '0' + pow) {
              // cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
              int s = d[p.first][p.second];
              field[ny][nx] = '.';
              for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                  d[i][j] = INF;
                }
              }

              while (que.size()) {
                que.pop();
              }
              que.push(P(ny, nx));
              d[ny][nx] = s + 1;
              if (pow == N) {
                field[ny][nx] = '0' + N;
              }
              pow++;
              break;
            } else {
              que.push(P(ny, nx));
              d[ny][nx] = d[p.first][p.second] + 1;
            }
          }
        }
      }
    }

    std::cout << count << std::endl;
    break;
  }

  return 0;
}