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

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

#define INF 999999999
#define MAX_X 1000
#define MAX_Y 1000
#define MAX_HEALTH 10
struct status {
  int health;
  int x;
  int y;
  int minutes;
};

queue<status> bfs;
bool checked[12][1010][1010]; // health,x,y
// int lastnum[11] = {0,0,0,0,0,0,0,0,0,0};
int maxhp = 0;
/// queue???push??????
///??¢??????????????\\??????????????¨?????????status?????´?????????????????????
bool PushToQueue(status a) {
  /*for (int i = 0;i < lastnum[a.health];i++) {
          if (checked[i].health == a.health && checked[a.health][i].x == a.x &&
  checked[a.health][i].y == a.y && checked[a.health][i].minutes == a.minutes){
                  cout << "health:" << a.health << "/" << i << "/" <<
  lastnum[a.health] << endl; return true;
          }
  }
  checked[a.health][lastnum[a.health]++] = a;*/
  if (checked[a.health][a.x][a.y] == true)
    return false;
  if (a.health < maxhp)
    return false;
  bfs.push(a);
  if (a.health > maxhp) {
    maxhp = a.health;
    // cout << "maxhp has changed to " << maxhp << endl;
  }
  checked[a.health][a.x][a.y] = true;
  return true;
}

int main() {
  for (int a = 0; a < 11; a++) {
    for (int b = 0; b < 1000; b++) {
      for (int c = 0; c < 1000; c++) {
        checked[a][b][c] = false;
      }
    }
  }

  int H, W, N;

  cin >> H >> W >> N;
  char data[H][W];

  for (int i = 0; i < H; i++) {
    cin >> data[i];
  }
  // cout << "A" << endl;
  // Find 'S'
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (data[y][x] == 'S') {
        bfs.push((struct status){1, x, y, 0});
        goto getout;
      }
    }
  }

getout:
  int result = INF;
  for (;;) {
    if (bfs.empty())
      break;
    struct status popped = bfs.front();
    bfs.pop();
    // cout << popped.health << "/" << popped.x << "/" << popped.y << "/" <<
    // popped.minutes << "/" << endl;
    if (popped.health == N + 1) {
      if (popped.minutes < result) {
        result = popped.minutes;
      }
    } else {
      for (int x = -1; x < 2; x++) {
        for (int y = -1; y < 2; y++) {
          if (x != 0 && y != 0)
            continue;
          if (popped.x + x < 0 || popped.x + x >= W || popped.y + y < 0 ||
              popped.y + y >= H)
            continue;
          if (data[popped.y + y][popped.x + x] == 'X')
            continue;
          else if (data[popped.y + y][popped.x + x] == '.') {
            PushToQueue((struct status){popped.health, popped.x + x,
                                        popped.y + y, popped.minutes + 1});
          } else {
            // cout << "*****************************factory" << endl;
            int factory = data[popped.y + y][popped.x + x] - 48;
            // cout << data[popped.y + y][popped.x + x]<< endl;
            if (factory == popped.health) {
              // eat
              PushToQueue((struct status){popped.health + 1, popped.x + x,
                                          popped.y + y, popped.minutes + 1});
            } else {
              // the mouse doesn't eat.
              PushToQueue((struct status){popped.health, popped.x + x,
                                          popped.y + y, popped.minutes + 1});
            }
          }
        }
      }
    }
  }
  cout << result << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define MAX_DATA 100000
struct status {
  int health;
  int x;
  int y;
  int minutes;
  int bx;
  int by;
};

queue<status> bfs;
// status checked[11][MAX_DATA];//
// int lastnum[11] = {0,0,0,0,0,0,0,0,0,0};
int maxhp = 0;
/// queue???push??????
///??¢??????????????\\??????????????¨?????????status?????´?????????????????????
bool PushToQueue(status a) {
  /*for (int i = 0;i < lastnum[a.health];i++) {
          if (checked[i].health == a.health && checked[a.health][i].x == a.x &&
  checked[a.health][i].y == a.y && checked[a.health][i].minutes == a.minutes){
                  cout << "health:" << a.health << "/" << i << "/" <<
  lastnum[a.health] << endl; return true;
          }
  }
  checked[a.health][lastnum[a.health]++] = a;*/
  if (a.health < maxhp)
    return false;
  bfs.push(a);
  if (a.health > maxhp)
    maxhp = a.health;

  return true;
}

int main() {
  int H, W, N;

  cin >> H >> W >> N;
  char data[H][W];

  for (int i = 0; i < H; i++) {
    cin >> data[i];
  }
  // Find 'S'
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (data[y][x] == 'S') {
        bfs.push((struct status){1, x, y, 0, -100, -100});
        goto getout;
      }
    }
  }
getout:
  int result;
  for (;;) {
    if (bfs.empty())
      break;
    struct status popped = bfs.front();
    bfs.pop();
    // cout << popped.health << "/" << popped.x << "/" << popped.y << "/" <<
    // popped.minutes << "/" << popped.bx << "/" << popped.by << endl;
    if (popped.health == N + 1) {
      result = popped.minutes;
      break;
    } else {
      for (int x = -1; x < 2; x++) {
        for (int y = -1; y < 2; y++) {
          if (x != 0 && y != 0)
            continue;
          if (popped.x + x < 0 || popped.x + x >= W || popped.y + y < 0 ||
              popped.y + y >= H)
            continue;
          if (popped.x + x == popped.bx && popped.y + y == popped.by)
            continue;
          if (data[popped.y + y][popped.x + x] == 'S' ||
              data[popped.y + y][popped.x + x] == 'X')
            continue;
          else if (data[popped.y + y][popped.x + x] == '.') {
            PushToQueue((struct status){popped.health, popped.x + x,
                                        popped.y + y, popped.minutes + 1,
                                        popped.x, popped.y});
          } else {
            // cout << "*****************************factory" << endl;
            int factory = data[popped.y + y][popped.x + x] - 48;
            // cout << data[popped.y + y][popped.x + x]<< endl;
            if (factory == popped.health) {
              // eat
              PushToQueue((struct status){popped.health + 1, popped.x + x,
                                          popped.y + y, popped.minutes + 1,
                                          popped.x, popped.y});
            } else {
              // the mouse doesn't eat.
              PushToQueue((struct status){popped.health, popped.x + x,
                                          popped.y + y, popped.minutes + 1,
                                          popped.x, popped.y});
            }
          }
        }
      }
    }
  }
  cout << result << endl;
  return 0;
}