Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define swap(a, b, type)                                                       \
  {                                                                            \
    type _tmp = a;                                                             \
    a = b;                                                                     \
    b = _tmp;                                                                  \
  }
typedef long long ll;
typedef unsigned long long ull;

struct operation {
  int y, x, yp, xp;
};

int a[501][501];
operation op[250000];

int main() {
  int H, W, N = 0;

  cin >> H >> W;
  repc(i, 1, H) {
    repc(j, 1, W) { cin >> a[i][j]; }
  }

  repc(i, 1, H) {
    repc(j, 1, W) {
      if (a[i][j] % 2 != 0) {
        if (j != W) {
          op[N].y = i;
          op[N].x = j;
          op[N].yp = i;
          op[N].xp = j + 1;

          N++;
          a[i][j]--;
          a[i][j + 1]++;
        } else if (1 != H) {
          op[N].y = i;
          op[N].x = j;
          op[N].yp = i + 1;
          op[N].xp = j;

          N++;
          a[i][j]--;
          a[i + 1][j]++;
        }
      }
    }
  }

  cout << N << endl;
  rep(i, N) printf("%d %d %d %d\n", op[i].y, op[i].x, op[i].yp, op[i].xp);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define swap(a, b, type)                                                       \
  {                                                                            \
    type _tmp = a;                                                             \
    a = b;                                                                     \
    b = _tmp;                                                                  \
  }
typedef long long ll;
typedef unsigned long long ull;

struct operation {
  int y, x, yp, xp;
};

int a[501][501];
operation op[250000];

int main() {
  int H, W, N = 0;

  cin >> H >> W;
  repc(i, 1, H) {
    repc(j, 1, W) { cin >> a[i][j]; }
  }

  repc(i, 1, H) {
    repc(j, 1, W) {
      if (a[i][j] % 2 != 0) {
        if (j != W) {
          op[N].y = i;
          op[N].x = j;
          op[N].yp = i;
          op[N].xp = j + 1;

          N++;
          a[i][j]--;
          a[i][j + 1]++;
        } else if (i != H) {
          op[N].y = i;
          op[N].x = j;
          op[N].yp = i + 1;
          op[N].xp = j;

          N++;
          a[i][j]--;
          a[i + 1][j]++;
        }
      }
    }
  }

  cout << N << endl;
  rep(i, N) printf("%d %d %d %d\n", op[i].y, op[i].x, op[i].yp, op[i].xp);

  return 0;
}