Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define BIT(a) (1 << (a))
#define EPS (1e-10)

using namespace std;
// using namespace boost::multiprecision;

const long long MOD = 1000000007;
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  bool same(int x, int y) { return findSet(x) == findSet(y); }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(a * x, a * y); }
  Point operator/(double a) { return Point(x / a, y / a); }

  bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; }

  double norm() { return x * x + y * y; }
};
typedef Point Vector;
typedef vector<Vector> Polygon;

double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > EPS)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) > -EPS)
    return CLOCKWISE;
  if (dot(a, b) < -EPS)
    return ONLINE_BACK;
  if (a.norm() < b.norm())
    return ONLINE_FRONT;

  return ON_SEGMENT;
}

Polygon andrewScan(Polygon s) {
  Polygon u, l;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());
  u.push_back(s[0]);
  u.push_back(s[1]);
  l.push_back(s[s.size() - 1]);
  l.push_back(s[s.size() - 2]);

  for (size_t i = 2; i < s.size(); i++) {
    for (size_t n = u.size();
         n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) {
      u.pop_back();
    }
    u.push_back(s[i]);
  }

  for (int i = (int)s.size() - 3; i >= 0; i--) {
    for (size_t n = l.size();
         n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) {
      l.pop_back();
    }
    l.push_back(s[i]);
  }

  reverse(l.begin(), l.end());
  for (size_t i = u.size() - 2; i >= 1; i--)
    l.push_back(u[i]);

  return l;
}

long long mss(long long A[], int l, int r) {
  int left;
  int right;
  long long ma;
  long long ma2;
  for (int i = l; i < r; i++) {
    if (i == l) {
      ma = A[i];
      ma2 = A[i];
      continue;
    }
    ma2 = max(A[i], ma2 + A[i]);
    ma = max(ma2, ma);
  }
  return ma;
}

long long mod_pow(long long x, long long n) {
  long long res = 1;
  for (int i = 0; i < 60; i++) {
    if (n >> i & 1)
      res = res * x % MOD;
    x = x * x % MOD;
  }
  return res;
}

#define Int long long
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// field[H][W]
int field[505][505];
int ans[100000][4];

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

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

  for (int i = 1; i <= H; i++) {
    if (i % 2) {
      for (int j = 1; j <= W; j++) {
        if (field[i][j] % 2) {
          if (j != W) {
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i;
            ans[N][3] = j + 1;
            N++;
            field[i][j] -= 1;
            field[i][j + 1] += 1;
          } else {
            if (i == H)
              break;
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i + 1;
            ans[N][3] = j;
            N++;

            field[i][j] -= 1;
            field[i + 1][j] += 1;
          }
        }
      }
    } else {
      for (int j = W; j >= 1; j--) {
        if (field[i][j] % 2) {
          if (j != 1) {
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i;
            ans[N][3] = j - 1;
            N++;
            field[i][j] -= 1;
            field[i][j - 1] += 1;
          } else {
            if (i == H)
              break;
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i + 1;
            ans[N][3] = j;
            N++;
            field[i][j] -= 1;
            field[i + 1][j] += 1;
          }
        }
      }
    }
  }
  cout << N << endl;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[i][j];
      if (j == 3)
        cout << endl;
      else
        cout << " ";
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

#define BIT(a) (1 << (a))
#define EPS (1e-10)

using namespace std;
// using namespace boost::multiprecision;

const long long MOD = 1000000007;
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  bool same(int x, int y) { return findSet(x) == findSet(y); }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

class Point {
public:
  double x, y;

  Point(double x = 0, double y = 0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(a * x, a * y); }
  Point operator/(double a) { return Point(x / a, y / a); }

  bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; }

  double norm() { return x * x + y * y; }
};
typedef Point Vector;
typedef vector<Vector> Polygon;

double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }

int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if (cross(a, b) > EPS)
    return COUNTER_CLOCKWISE;
  if (cross(a, b) > -EPS)
    return CLOCKWISE;
  if (dot(a, b) < -EPS)
    return ONLINE_BACK;
  if (a.norm() < b.norm())
    return ONLINE_FRONT;

  return ON_SEGMENT;
}

Polygon andrewScan(Polygon s) {
  Polygon u, l;
  if (s.size() < 3)
    return s;
  sort(s.begin(), s.end());
  u.push_back(s[0]);
  u.push_back(s[1]);
  l.push_back(s[s.size() - 1]);
  l.push_back(s[s.size() - 2]);

  for (size_t i = 2; i < s.size(); i++) {
    for (size_t n = u.size();
         n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) {
      u.pop_back();
    }
    u.push_back(s[i]);
  }

  for (int i = (int)s.size() - 3; i >= 0; i--) {
    for (size_t n = l.size();
         n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) == COUNTER_CLOCKWISE; n--) {
      l.pop_back();
    }
    l.push_back(s[i]);
  }

  reverse(l.begin(), l.end());
  for (size_t i = u.size() - 2; i >= 1; i--)
    l.push_back(u[i]);

  return l;
}

long long mss(long long A[], int l, int r) {
  int left;
  int right;
  long long ma;
  long long ma2;
  for (int i = l; i < r; i++) {
    if (i == l) {
      ma = A[i];
      ma2 = A[i];
      continue;
    }
    ma2 = max(A[i], ma2 + A[i]);
    ma = max(ma2, ma);
  }
  return ma;
}

long long mod_pow(long long x, long long n) {
  long long res = 1;
  for (int i = 0; i < 60; i++) {
    if (n >> i & 1)
      res = res * x % MOD;
    x = x * x % MOD;
  }
  return res;
}

#define Int long long
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// field[H][W]
int field[505][505];
int ans[2550250][4];

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

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

  for (int i = 1; i <= H; i++) {
    if (i % 2) {
      for (int j = 1; j <= W; j++) {
        if (field[i][j] % 2) {
          if (j != W) {
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i;
            ans[N][3] = j + 1;
            N++;
            field[i][j] -= 1;
            field[i][j + 1] += 1;
          } else {
            if (i == H)
              break;
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i + 1;
            ans[N][3] = j;
            N++;

            field[i][j] -= 1;
            field[i + 1][j] += 1;
          }
        }
      }
    } else {
      for (int j = W; j >= 1; j--) {
        if (field[i][j] % 2) {
          if (j != 1) {
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i;
            ans[N][3] = j - 1;
            N++;
            field[i][j] -= 1;
            field[i][j - 1] += 1;
          } else {
            if (i == H)
              break;
            ans[N][0] = i;
            ans[N][1] = j;
            ans[N][2] = i + 1;
            ans[N][3] = j;
            N++;
            field[i][j] -= 1;
            field[i + 1][j] += 1;
          }
        }
      }
    }
  }
  cout << N << endl;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 4; j++) {
      cout << ans[i][j];
      if (j == 3)
        cout << endl;
      else
        cout << " ";
    }
  }
}