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

// 2011/09/22 Tazoe

#include <iostream>
using namespace std;

void DFS(char B[2010][2010], const int Ny, const int Nx, int y, int x,
         const char c) {
  if (B[y][x] != '-')
    return;

  B[y][x] = c;

  if (y - 1 >= 0)
    DFS(B, Ny, Nx, y - 1, x, c);
  if (y + 1 <= Ny - 2)
    DFS(B, Ny, Nx, y + 1, x, c);
  if (x - 1 >= 0)
    DFS(B, Ny, Nx, y, x - 1, c);
  if (x + 1 <= Nx - 2)
    DFS(B, Ny, Nx, y, x + 1, c);
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    char B[2010][2010];
    int X[2010];
    int Y[2010];
    int Nx;
    int Ny;

    B[0][0] = '-';
    B[0][1] = '-';
    B[1][0] = '-';
    B[1][1] = '-';
    X[0] = 0;
    X[1] = w;
    Y[0] = 0;
    Y[1] = h;
    Nx = 2;
    Ny = 2;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;

      int X1 = 0;
      while (X[X1] < x1)
        X1++;

      if (X[X1] > x1) {
        for (int x = Nx; x >= X1; x--) {
          for (int y = 0; y < Ny; y++)
            B[y][x] = B[y][x - 1];
          X[x] = X[x - 1];
        }
        X[X1] = x1;
        Nx++;
      }

      int X2 = X1;
      while (X[X2] < x2)
        X2++;

      if (X[X2] > x2) {
        for (int x = Nx; x >= X2; x--) {
          for (int y = 0; y < Ny; y++)
            B[y][x] = B[y][x - 1];
          X[x] = X[x - 1];
        }
        X[X2] = x2;
        Nx++;
      }

      int Y1 = 0;
      while (Y[Y1] < y1)
        Y1++;

      if (Y[Y1] > y1) {
        for (int y = Ny; y >= Y1; y--) {
          for (int x = 0; x < Nx; x++)
            B[y][x] = B[y - 1][x];
          Y[y] = Y[y - 1];
        }
        Y[Y1] = y1;
        Ny++;
      }

      int Y2 = Y1;
      while (Y[Y2] < y2)
        Y2++;

      if (Y[Y2] > y2) {
        for (int y = Ny; y >= Y2; y--) {
          for (int x = 0; x < Nx; x++)
            B[y][x] = B[y - 1][x];
          Y[y] = Y[y - 1];
        }
        Y[Y2] = y2;
        Ny++;
      }

      for (int y = Y1; y < Y2; y++)
        for (int x = X1; x < X2; x++)
          B[y][x] = '*';
    }
    /*
                    for(int y=Ny-2; y>=0; y--){
                            for(int x=0; x<Nx-1; x++)
                                    cout << B[y][x];
                            cout << endl;
                    }
    */
    int cnt = 0;

    for (int y = 0; y < Ny - 1; y++)
      for (int x = 0; x < Nx - 1; x++)
        if (B[y][x] == '-') {
          cnt++;
          DFS(B, Ny, Nx, y, x, '/');
        }

    cout << cnt << endl;
    /*
                    for(int y=Ny-2; y>=0; y--){
                            for(int x=0; x<Nx-1; x++)
                                    cout << B[y][x];
                            cout << endl;
                    }
    */
  }

  return 0;
}

Error: 0

Fixed submission:

// 2011/09/22 Tazoe

#include <iostream>
using namespace std;

void DFS(short B[2010][2010], const int Ny, const int Nx, int y, int x, int c) {
  if (B[y][x] != 0)
    return;

  B[y][x] = c;

  if (y - 1 >= 0)
    DFS(B, Ny, Nx, y - 1, x, c);
  if (y + 1 <= Ny - 2)
    DFS(B, Ny, Nx, y + 1, x, c);
  if (x - 1 >= 0)
    DFS(B, Ny, Nx, y, x - 1, c);
  if (x + 1 <= Nx - 2)
    DFS(B, Ny, Nx, y, x + 1, c);
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    short B[2010][2010];
    int X[2010];
    int Y[2010];
    int Nx;
    int Ny;

    B[0][0] = 0;
    B[0][1] = 0;
    B[1][0] = 0;
    B[1][1] = 0;
    X[0] = 0;
    X[1] = w;
    Y[0] = 0;
    Y[1] = h;
    Nx = 2;
    Ny = 2;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;

      int X1 = 0;
      while (X[X1] < x1)
        X1++;

      if (X[X1] > x1) {
        for (int x = Nx; x >= X1; x--) {
          for (int y = 0; y < Ny; y++)
            B[y][x] = B[y][x - 1];
          X[x] = X[x - 1];
        }
        X[X1] = x1;
        Nx++;
      }

      int X2 = X1;
      while (X[X2] < x2)
        X2++;

      if (X[X2] > x2) {
        for (int x = Nx; x >= X2; x--) {
          for (int y = 0; y < Ny; y++)
            B[y][x] = B[y][x - 1];
          X[x] = X[x - 1];
        }
        X[X2] = x2;
        Nx++;
      }

      int Y1 = 0;
      while (Y[Y1] < y1)
        Y1++;

      if (Y[Y1] > y1) {
        for (int y = Ny; y >= Y1; y--) {
          for (int x = 0; x < Nx; x++)
            B[y][x] = B[y - 1][x];
          Y[y] = Y[y - 1];
        }
        Y[Y1] = y1;
        Ny++;
      }

      int Y2 = Y1;
      while (Y[Y2] < y2)
        Y2++;

      if (Y[Y2] > y2) {
        for (int y = Ny; y >= Y2; y--) {
          for (int x = 0; x < Nx; x++)
            B[y][x] = B[y - 1][x];
          Y[y] = Y[y - 1];
        }
        Y[Y2] = y2;
        Ny++;
      }

      for (int y = Y1; y < Y2; y++)
        for (int x = X1; x < X2; x++)
          B[y][x] = -1;
    }

    int cnt = 0;

    for (int y = 0; y < Ny - 1; y++)
      for (int x = 0; x < Nx - 1; x++)
        if (B[y][x] == 0)
          DFS(B, Ny, Nx, y, x, ++cnt);

    cout << cnt << endl;
    /*
                    for(int y=Ny-2; y>=0; y--){
                            for(int x=0; x<Nx-1; x++)
                                    if(B[y][x]==-1)
                                            cout << '*';
                                    else
                                            cout << B[y][x];
                            cout << endl;
                    }
    */
  }

  return 0;
}