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

#include <iostream>
#include <queue>
#include <utility>
using namespace std;

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W;
char N;
const int MAX_H = 1010, MAX_W = 1010;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
  queue<Pair> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  char muscle = '1';
  que.push(Pair(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    Pair p = que.front();
    que.pop();
    if (muscle - 1 == N)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(Pair(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
      if (muscle == plant[nx][ny]) {
        while (que.size()) {
          que.pop();
        }
        que.push(Pair(nx, ny));
        muscle = muscle + 1;
        mytime += d[nx][ny];
        for (int i = 0; i < H; i++) {
          for (int j = 0; j < W; j++) {
            d[i][j] = INF;
          }
        }
        d[nx][ny] = 0;
        break;
      }
    }
  }
}

void solve() {
  mytime = 0;
  bfs();
  cout << mytime << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  // IO
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> plant[i][j];
      if (plant[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  solve();
}

// 错误版本突然又正确了，这是什么情况啊？？？？？？？！！！！！！！！


Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
using namespace std;

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W;
char N;
const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int d[MAX_H][MAX_W];
int mytime;

void bfs() {
  queue<Pair> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  char muscle = '1';
  que.push(Pair(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    Pair p = que.front();
    que.pop();
    if (muscle - 1 == N)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(Pair(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
      if (muscle == plant[nx][ny]) {
        while (que.size()) {
          que.pop();
        }
        que.push(Pair(nx, ny));
        muscle = muscle + 1;
        mytime += d[nx][ny];
        for (int i = 0; i < H; i++) {
          for (int j = 0; j < W; j++) {
            d[i][j] = INF;
          }
        }
        d[nx][ny] = 0;
        break;
      }
    }
  }
}

void solve() {
  mytime = 0;
  bfs();
  cout << mytime << endl;
}

int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> plant[i][j];
      if (plant[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  solve();
}