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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

struct Data {
  int y, x;
  Data() {}
  Data(int _y, int _x) {
    y = _y;
    x = _x;
  }
};

int pW, pH, N;
bool fld[6000][6000];
const int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};

int press(int x[][1000], int W) {
  vector<int> x_c;
  for (int i = 0; i < N; i++) {
    x_c.push_back(x[0][i]);
    x_c.push_back(x[1][i]);
    if (x[0][i] != 0)
      x_c.push_back(x[0][i] - 1);
    if (x[1][i] != 0)
      x_c.push_back(x[1][i] - 1);
    if (x[0][i] != W - 1)
      x_c.push_back(x[0][i] + 1);
    if (x[1][i] != W - 1)
      x_c.push_back(x[1][i] + 1);
  }
  sort(x_c.begin(), x_c.end());
  x_c.erase(unique(x_c.begin(), x_c.end()), x_c.end());
  // for(int i=0;i<x_c.size();i++) printf(" %d",x_c[i]); puts("");
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < N; j++) {
      x[i][j] = find(x_c.begin(), x_c.end(), x[i][j]) - x_c.begin();
    }
  }
  return x_c.size();
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    int x[2][1000], y[2][1000];
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x[0][i] >> y[0][i] >> x[1][i] >> y[1][i];
      x[1][i]--;
      y[1][i]--;
    }
    pW = press(x, W);
    pH = press(y, H);
    for (int i = 0; i < pH; i++)
      for (int j = 0; j < pW; j++)
        fld[i][j] = false;
    for (int k = 0; k < N; k++) {
      for (int i = y[0][k]; i <= y[1][k]; i++) {
        for (int j = x[0][k]; j <= x[1][k]; j++) {
          fld[i][j] = true;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < pH; i++) {
      for (int j = 0; j < pW; j++) {
        // printf("%d ",fld[i][j]);
        if (fld[i][j])
          continue;
        fld[i][j] = true;
        ans++;

        stack<Data> s;
        s.push(Data(i, j));
        Data s_c;
        while (!s.empty()) {
          s_c = s.top();
          s.pop();
          for (int k = 0; k < 4; k++) {
            int ny = s_c.y + dy[k], nx = s_c.x + dx[k];
            if (ny < 0 || pH <= ny || nx < 0 || pW <= nx || fld[ny][nx])
              continue;
            fld[ny][nx] = true;
            s.push(Data(ny, nx));
          }
        }
      } // puts("");
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int pW, pH, N;
bool fld[6000][6000];
const int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};

void dfs(int y, int x) {
  int ny, nx;
  for (int i = 0; i < 4; i++) {
    ny = y + dy[i];
    nx = x + dx[i];
    if (ny < 0 || pH <= ny || nx < 0 || pW <= nx || fld[ny][nx])
      continue;
    fld[ny][nx] = true;
    dfs(ny, nx);
  }
  return;
}

int press(int x[][1000], int W) {
  vector<int> x_c;
  for (int i = 0; i < N; i++) {
    x_c.push_back(x[0][i]);
    x_c.push_back(x[1][i]);
    if (x[0][i] != 0)
      x_c.push_back(x[0][i] - 1);
    if (x[1][i] != 0)
      x_c.push_back(x[1][i] - 1);
    if (x[0][i] != W - 1)
      x_c.push_back(x[0][i] + 1);
    if (x[1][i] != W - 1)
      x_c.push_back(x[1][i] + 1);
  }
  sort(x_c.begin(), x_c.end());
  x_c.erase(unique(x_c.begin(), x_c.end()), x_c.end());
  // for(int i=0;i<x_c.size();i++) printf(" %d",x_c[i]); puts("");
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < N; j++) {
      x[i][j] = find(x_c.begin(), x_c.end(), x[i][j]) - x_c.begin();
    }
  }
  return x_c.size();
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    int x[2][1000], y[2][1000];
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x[0][i] >> y[0][i] >> x[1][i] >> y[1][i];
      x[1][i]--;
      y[1][i]--;
    }
    pW = press(x, W);
    pH = press(y, H);
    for (int i = 0; i < pH; i++)
      for (int j = 0; j < pW; j++)
        fld[i][j] = false;
    for (int k = 0; k < N; k++) {
      for (int i = y[0][k]; i <= y[1][k]; i++) {
        for (int j = x[0][k]; j <= x[1][k]; j++) {
          fld[i][j] = true;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < pH; i++) {
      for (int j = 0; j < pW; j++) {
        // printf("%d ",fld[i][j]);
        if (fld[i][j])
          continue;
        fld[i][j] = true;
        dfs(i, j);
        ans++;
      } // puts("");
    }
    printf("%d\\n", ans);
  }
  return 0;
}