Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e18 + 7;

int H, W;
int a[505][505];
int go[2505][5];

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }

  int N = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2) {
      for (int j = W - 1; j >= 0; j--) {
        if (j == W - 1) {
          if (a[i - 1][j] % 2) {
            a[i - 1][j]--;
            a[i][j]++;
            go[N][0] = i - 1;
            go[N][1] = j;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        } else {
          if (a[i][j + 1] % 2) {
            a[i][j + 1]--;
            a[i][j]++;
            go[N][0] = i;
            go[N][1] = j + 1;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        }
      }
    } else {
      for (int j = 0; j < W; j++) {
        if (i == 0 && j == 0)
          continue;
        if (j == 0) {
          if (a[i - 1][j] % 2) {
            a[i - 1][j]--;
            a[i][j]++;
            go[N][0] = i - 1;
            go[N][1] = j;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        } else {
          if (a[i][j - 1] % 2) {
            a[i][j - 1]--;
            a[i][j]++;
            go[N][0] = i;
            go[N][1] = j - 1;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        }
      }
    }
  }

  cout << N << endl;
  for (int i = 0; i < N; i++) {
    cout << go[i][0] + 1 << " " << go[i][1] + 1 << " " << go[i][2] + 1 << " "
         << go[i][3] + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e18 + 7;

int H, W;
int a[505][505];
int go[300000][5];

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
    }
  }

  int N = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2) {
      for (int j = W - 1; j >= 0; j--) {
        if (j == W - 1) {
          if (a[i - 1][j] % 2) {
            a[i - 1][j]--;
            a[i][j]++;
            go[N][0] = i - 1;
            go[N][1] = j;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        } else {
          if (a[i][j + 1] % 2) {
            a[i][j + 1]--;
            a[i][j]++;
            go[N][0] = i;
            go[N][1] = j + 1;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        }
      }
    } else {
      for (int j = 0; j < W; j++) {
        if (i == 0 && j == 0)
          continue;
        if (j == 0) {
          if (a[i - 1][j] % 2) {
            a[i - 1][j]--;
            a[i][j]++;
            go[N][0] = i - 1;
            go[N][1] = j;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        } else {
          if (a[i][j - 1] % 2) {
            a[i][j - 1]--;
            a[i][j]++;
            go[N][0] = i;
            go[N][1] = j - 1;
            go[N][2] = i;
            go[N][3] = j;
            N++;
          }
        }
      }
    }
  }

  cout << N << endl;
  for (int i = 0; i < N; i++) {
    cout << go[i][0] + 1 << " " << go[i][1] + 1 << " " << go[i][2] + 1 << " "
         << go[i][3] + 1 << endl;
  }
}