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

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

int H;
int b[10][5];

struct range {
  int row;
  int left;
  int right;
};

void drop(int row, int col) {
  for (int i = row; i > 0; i--) {
    b[i][col] = b[i - 1][col];
  }
  b[0][col] = -1;
}

void print_board() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < 5; j++) {
      cerr << b[i][j] << ' ';
    }
    cerr << endl;
  }
  cerr << endl;
}

void solve() {
  int ans = 0;
  while (1) {
    /* print_board(); */
    vector<range> deleted;
    for (int i = 0; i < H; i++) {
      int left = 0;
      for (int j = 1; j < 6; j++) {
        if (j == 5 || b[i][j] != b[i][j - 1]) {
          if (j >= left + 3) {
            if (b[i][j - 1] != -1) {
              range tmp = {i, left, j - 1};
              deleted.push_back(tmp);
              ans += b[i][j - 1] * (j - left);
            }
            break;
          } else {
            left = j;
          }
        }
      }
    }
    if (deleted.size() == 0) {
      break;
    }
    for (int k = 0; k < deleted.size(); k++) {
      range d = deleted[k];
      for (int j = d.left; j <= d.right; j++) {
        drop(d.row, j);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while (1) {
    cin >> H;
    if (H == 0) {
      break;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 5; j++) {
        cin >> b[i][j];
      }
    }
    solve();
  }
}


Error: 0
6 9 9 9 9 

6 -1 -1 -1 -1 

5 9 5 5 9 
5 5 6 9 9 
4 6 3 6 9 
3 3 2 9 9 
2 2 1 1 1 

5 9 -1 -1 -1 
5 5 5 5 9 
4 6 6 9 9 
3 3 3 6 9 
2 2 2 9 9 

-1 -1 -1 -1 -1 
-1 -1 -1 -1 9 
-1 -1 -1 9 9 
5 9 -1 6 9 
4 6 6 9 9 

3 5 6 5 6 
2 2 2 8 3 
6 2 5 9 2 
7 7 7 6 1 
4 6 6 4 9 
8 9 1 1 8 
5 6 1 8 1 
6 8 2 1 2 
9 6 3 3 5 
5 3 8 8 8 

-1 -1 -1 -1 -1 
-1 -1 -1 5 6 
3 5 -1 8 3 
6 2 6 9 2 
4 6 5 6 1 
8 9 6 4 9 
5 6 1 1 8 
6 8 1 8 1 
9 6 2 1 2 
5 3 3 3 5 

-1 -1 -1 -1 -1 
-1 -1 -1 -1 6 
3 -1 -1 5 3 
6 5 -1 8 2 
4 2 6 9 1 
8 6 5 6 9 
5 9 6 4 8 
6 6 1 1 1 
9 8 1 8 2 
5 6 2 1 5 

-1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 
3 -1 -1 -1 6 
6 5 -1 5 3 
4 2 -1 8 2 
8 6 6 9 1 
5 9 5 6 9 
6 6 6 4 8 
9 8 1 8 2 
5 6 2 1 5 

-1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 
-1 -1 -1 -1 6 
3 -1 -1 5 3 
6 5 -1 8 2 
4 2 -1 9 1 
8 6 6 6 9 
5 9 5 4 8 
9 8 1 8 2 
5 6 2 1 5 

-1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 
-1 -1 -1 -1 6 
3 -1 -1 -1 3 
6 -1 -1 5 2 
4 5 -1 8 1 
8 2 -1 9 9 
5 9 5 4 8 
9 8 1 8 2 
5 6 2 1 5 

1 2 3 4 5 
6 7 8 9 1 
2 3 4 5 6 
7 8 9 1 2 
3 4 5 6 7 

2 2 8 7 4 
6 5 7 7 7 
8 8 9 9 9 

2 2 -1 -1 -1 
6 5 -1 -1 -1 
8 8 8 7 4 

-1 -1 -1 -1 -1 
2 2 -1 -1 -1 
6 5 -1 7 4 



Fixed submission:

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

int H;
int b[10][5];

struct range {
  int row;
  int left;
  int right;
};

void drop(int row, int col) {
  for (int i = row; i > 0; i--) {
    b[i][col] = b[i - 1][col];
  }
  b[0][col] = -1;
}

void print_board() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < 5; j++) {
      cerr << b[i][j] << ' ';
    }
    cerr << endl;
  }
  cerr << endl;
}

void solve() {
  int ans = 0;
  while (1) {
    print_board();
    vector<range> deleted;
    for (int i = 0; i < H; i++) {
      int left = 0;
      for (int j = 1; j < 6; j++) {
        if (j == 5 || b[i][j] != b[i][j - 1]) {
          if (j >= left + 3) {
            if (b[i][j - 1] != -1) {
              range tmp = {i, left, j - 1};
              deleted.push_back(tmp);
              ans += b[i][j - 1] * (j - left);
            }
            break;
          } else {
            left = j;
          }
        }
      }
    }
    if (deleted.size() == 0) {
      break;
    }
    for (int k = 0; k < deleted.size(); k++) {
      range d = deleted[k];
      for (int j = d.left; j <= d.right; j++) {
        drop(d.row, j);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  while (1) {
    cin >> H;
    if (H == 0) {
      break;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 5; j++) {
        cin >> b[i][j];
      }
    }
    solve();
  }
}