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

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int W, H, N;
int X1[5000];
int X2[5000];
int Y1[5000];
int Y2[5000];
bool field[5001][5001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
queue<pair<int, int>> que;
int main() {
  while (true) {
    for (int i = 0; i < 5001; i++)
      for (int j = 0; j < 5001; j++)
        field[i][j] = false;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      return 0;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
    for (int i = 0; i < N; i++) {
      for (int x = X1[i]; x < X2[i]; x++) {
        for (int y = Y1[i]; y < Y2[i]; y++) {
          field[x][y] = true;
        }
      }
    }

    int ans = 0;
    for (int i = 0; i < W; i++) {
      for (int j = 0; j < H; j++) {
        if (field[i][j])
          continue;
        ans++;
        que.push(make_pair(i, j));
        while (!que.empty()) {
          pair<int, int> P = que.front();
          que.pop();
          int pf = P.first;
          int ps = P.second;
          if (field[pf][ps])
            continue;
          field[pf][ps] = true;
          for (int k = 0; k < 4; k++) {
            if (pf + dx[k] < 0)
              continue;
            if (pf + dx[k] >= W)
              continue;
            if (ps + dy[k] < 0)
              continue;
            if (ps + dy[k] >= H)
              continue;
            if (field[pf + dx[k]][ps + dy[k]])
              continue;
            que.push(make_pair(pf + dx[k], ps + dy[k]));
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int W, H, N;
int X1[1000];
int X2[1000];
int Y1[1000];
int Y2[1000];
bool field[1001][1001];
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
queue<pair<int, int>> que;
int main() {
  while (true) {
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        field[i][j] = false;
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      return 0;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
    for (int i = 0; i < N; i++) {
      for (int x = X1[i]; x < X2[i]; x++) {
        for (int y = Y1[i]; y < Y2[i]; y++) {
          field[x][y] = true;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < W; i++) {
      for (int j = 0; j < H; j++) {
        if (field[i][j])
          continue;
        ans++;
        que.push(make_pair(i, j));
        while (!que.empty()) {
          pair<int, int> P = que.front();
          que.pop();
          int pf = P.first;
          int ps = P.second;
          if (field[pf][ps])
            continue;
          field[pf][ps] = true;
          for (int k = 0; k < 4; k++) {
            if (pf + dx[k] < 0)
              continue;
            if (pf + dx[k] >= W)
              continue;
            if (ps + dy[k] < 0)
              continue;
            if (ps + dy[k] >= H)
              continue;
            if (field[pf + dx[k]][ps + dy[k]])
              continue;
            que.push(make_pair(pf + dx[k], ps + dy[k]));
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}