Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
const ll INF = 1e15;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int grid[h + 1][w + 1];
  rep(i, h) {
    rep(j, w) { cin >> grid[i][j]; }
  }
  int ans[505][4];
  int y = 0, x = 0, a, b, n = 0;
  bool re = 0, flag = 0;
  while (y <= h - 1 && x <= w - 1) {
    if (flag) {
      grid[y][x]++;
      ans[n][0] = a;
      ans[n][1] = b;
      ans[n][2] = y + 1;
      ans[n][3] = x + 1;
      n++;
      flag = 0;
    }
    if (grid[y][x] % 2) {
      grid[y][x]--;
      a = y + 1;
      b = x + 1;
      flag = 1;
    }
    if (y % 2 == 0 && x == w - 1) {
      y++;
      re = 1;
    } else if (y % 2 == 1 && x == 0) {
      y++;
      re = 0;
    } else if (re)
      x--;
    else
      x++;
  }
  PRINT(n);
  rep(i, n) {
    cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << " "
         << ans[i][3] << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
const ll INF = 1e15;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int grid[h + 1][w + 1];
  rep(i, h) {
    rep(j, w) { cin >> grid[i][j]; }
  }
  int ans[250005][4];
  int y = 0, x = 0, a, b, n = 0;
  bool re = 0, flag = 0;
  while (y <= h - 1 && x <= w - 1) {
    if (flag) {
      grid[y][x]++;
      ans[n][0] = a;
      ans[n][1] = b;
      ans[n][2] = y + 1;
      ans[n][3] = x + 1;
      n++;
      flag = 0;
    }
    if (grid[y][x] % 2) {
      grid[y][x]--;
      a = y + 1;
      b = x + 1;
      flag = 1;
    }
    if (y % 2 == 0 && x == w - 1) {
      y++;
      re = 1;
    } else if (y % 2 == 1 && x == 0) {
      y++;
      re = 0;
    } else if (re)
      x--;
    else
      x++;
  }
  PRINT(n);
  rep(i, n) {
    cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << " "
         << ans[i][3] << '\n';
  }
}