Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());
#define PAU system("pause")

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 30);
const double PI = acos(-1);

struct Move {
  int x, y, ax, ay;
  Move() {}
  Move(int _y, int _x, int _ay, int _ax) {
    x = _x;
    y = _y;
    ax = _ax;
    ay = _ay;
  }
};

int H, W;

bool check(int y, int x) {
  if (y < 0 || y >= H)
    return false;
  if (x < 0 || x >= W)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W;
  vvi A(9, vi(9, 0));
  REP(i, H) REP(j, W) { cin >> A[i][j]; }
  vector<Move> VM;
  REP(i, H) REP(j, W) {
    if (A[i][j] % 2 == 1) {
      if (!check(i, j + 1)) {
        // 必ず下に動かさなければならない
        if (!check(i + 1, j))
          continue;
        A[i][j] -= 1;
        A[i + 1][j] += 1;
        VM.push_back(Move(i + 1, j + 1, i + 2, j + 1));
      } else {
        A[i][j] -= 1;
        A[i][j + 1] += 1;
        VM.push_back(Move(i + 1, j + 1, i + 1, j + 2));
      }
    }
  }
  cout << SZ(VM) << endl;
  for (Move m : VM) {
    cout << m.y << " " << m.x << " " << m.ay << " " << m.ax << endl;
  }
  PAU;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());
#define PAU system("pause")

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 30);
const double PI = acos(-1);

struct Move {
  int x, y, ax, ay;
  Move() {}
  Move(int _y, int _x, int _ay, int _ax) {
    x = _x;
    y = _y;
    ax = _ax;
    ay = _ay;
  }
};

int H, W;

bool check(int y, int x) {
  if (y < 0 || y >= H)
    return false;
  if (x < 0 || x >= W)
    return false;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W;
  vvi A(H, vi(W, 0));
  REP(i, H) REP(j, W) { cin >> A[i][j]; }
  vector<Move> VM;
  REP(i, H) REP(j, W) {
    if (A[i][j] % 2 == 1) {
      if (!check(i, j + 1)) {
        // 必ず下に動かさなければならない
        if (!check(i + 1, j))
          continue;
        A[i][j] -= 1;
        A[i + 1][j] += 1;
        VM.push_back(Move(i + 1, j + 1, i + 2, j + 1));
      } else {
        A[i][j] -= 1;
        A[i][j + 1] += 1;
        VM.push_back(Move(i + 1, j + 1, i + 1, j + 2));
      }
    }
  }
  cout << SZ(VM) << endl;
  for (Move m : VM) {
    cout << m.y << " " << m.x << " " << m.ay << " " << m.ax << endl;
  }
  PAU;
  return 0;
}