Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int H, W;
int a[100000][4];
int main() {
  cin >> H >> W;
  int map[H][W];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> map[i][j];
  int s = 0, u = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < W; j++) {
        if (((s == 0 && map[i][j] % 2 == 1) ||
             (s == 1 && map[i][j] % 2 == 0)) &&
            (i != H - 1 || j != W - 1)) {
          a[u][0] = i;
          a[u][1] = j;
          if (j < W - 1) {
            a[u][2] = i;
            a[u][3] = j + 1;
          } else {
            a[u][2] = i + 1;
            a[u][3] = j;
          }
          u++;
          s = 1;
        } else if (s == 1 && map[i][j] % 2 == 1 && (i != H - 1 || j != W - 1)) {
          s = 0;
        }
      }
    } else {
      for (int j = W - 1; j >= 0; j--) {
        if (((s == 0 && map[i][j] % 2 == 1) ||
             (s == 1 && map[i][j] % 2 == 0)) &&
            (i != H - 1 || j != 0)) {
          a[u][0] = i;
          a[u][1] = j;
          if (j > 0) {
            a[u][2] = i;
            a[u][3] = j - 1;
          } else {
            a[u][2] = i + 1;
            a[u][3] = j;
          }
          u++;
          s = 1;
        } else if (s == 1 && map[i][j] % 2 == 1 && (i != H - 1 || j != 0)) {
          s = 0;
        }
      }
    }
  }
  cout << u << endl;
  for (int i = 0; i < u; i++) {
    for (int j = 0; j <= 3; j++) {
      if (j == 3)
        cout << a[i][j] + 1 << endl;
      else
        cout << a[i][j] + 1 << " ";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int H, W;
int a[1000000][4];
int main() {
  cin >> H >> W;
  int map[H][W];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> map[i][j];
  int s = 0, u = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < W; j++) {
        if (((s == 0 && map[i][j] % 2 == 1) ||
             (s == 1 && map[i][j] % 2 == 0)) &&
            (i != H - 1 || j != W - 1)) {
          a[u][0] = i;
          a[u][1] = j;
          if (j < W - 1) {
            a[u][2] = i;
            a[u][3] = j + 1;
          } else {
            a[u][2] = i + 1;
            a[u][3] = j;
          }
          u++;
          s = 1;
        } else if (s == 1 && map[i][j] % 2 == 1 && (i != H - 1 || j != W - 1)) {
          s = 0;
        }
      }
    } else {
      for (int j = W - 1; j >= 0; j--) {
        if (((s == 0 && map[i][j] % 2 == 1) ||
             (s == 1 && map[i][j] % 2 == 0)) &&
            (i != H - 1 || j != 0)) {
          a[u][0] = i;
          a[u][1] = j;
          if (j > 0) {
            a[u][2] = i;
            a[u][3] = j - 1;
          } else {
            a[u][2] = i + 1;
            a[u][3] = j;
          }
          u++;
          s = 1;
        } else if (s == 1 && map[i][j] % 2 == 1 && (i != H - 1 || j != 0)) {
          s = 0;
        }
      }
    }
  }
  cout << u << endl;
  for (int i = 0; i < u; i++) {
    for (int j = 0; j <= 3; j++) {
      if (j == 3)
        cout << a[i][j] + 1 << endl;
      else
        cout << a[i][j] + 1 << " ";
    }
  }
}