Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (int i = p; i < q; i++)
#define ll long long

ll CONS = 1e9 + 7;

using namespace std;

// ABC 109 D
int main(void) {

  ll h, w;
  cin >> h >> w;
  vector<vector<ll>> a = vector<vector<ll>>(h + 1, vector<ll>(w + 1));
  rep(i, 1, h + 1) { rep(j, 1, w + 1) cin >> a[i][j]; }
  vector<string> ans = vector<string>();

  ll row = 1, col = 1;
  ll count = 0;
  bool goRight = true;
  while (count < h * w) {
    ll rowNext, colNext;
    count++;
    if (goRight) {
      if (col == w && row % 2 == 1) {
        colNext = col;
        rowNext = row + 1;
        goRight = false;
      } else {
        colNext = col + 1;
        rowNext = row;
      }
    } else {
      if (col == 1 && row % 2 == 0) {
        colNext = col;
        rowNext = row + 1;
        goRight = true;
      } else {
        colNext = col - 1;
        rowNext = row;
      }
    }
    if (a[row][col] % 2 == 1 && row > 0 && col > 0 && row < h + 1 &&
        col < w + 1) {
      string s = to_string(row) + " " + to_string(col) + " " +
                 to_string(rowNext) + " " + to_string(colNext);
      ans.push_back(s);
      a[rowNext][colNext]++;
    }
    row = rowNext;
    col = colNext;
  }
  cout << ans.size() << endl;
  rep(i, 0, ans.size()) { cout << ans[i] << endl; }
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, q) for (int i = p; i < q; i++)
#define ll long long

ll CONS = 1e9 + 7;

using namespace std;

// ABC 109 D
int main(void) {

  ll h, w;
  cin >> h >> w;
  vector<vector<ll>> a = vector<vector<ll>>(h + 1, vector<ll>(w + 1));
  rep(i, 1, h + 1) { rep(j, 1, w + 1) cin >> a[i][j]; }
  vector<string> ans = vector<string>();

  ll row = 1, col = 1;
  ll count = 0;
  bool goRight = true;
  while (count < h * w) {
    ll rowNext, colNext;
    count++;
    if (goRight) {
      if (col == w && row % 2 == 1) {
        colNext = col;
        rowNext = row + 1;
        goRight = false;
      } else {
        colNext = col + 1;
        rowNext = row;
      }
    } else {
      if (col == 1 && row % 2 == 0) {
        colNext = col;
        rowNext = row + 1;
        goRight = true;
      } else {
        colNext = col - 1;
        rowNext = row;
      }
    }
    if (a[row][col] % 2 == 1 && rowNext > 0 && colNext > 0 && rowNext < h + 1 &&
        colNext < w + 1) {
      string s = to_string(row) + " " + to_string(col) + " " +
                 to_string(rowNext) + " " + to_string(colNext);
      ans.push_back(s);
      a[rowNext][colNext]++;
    }
    row = rowNext;
    col = colNext;
  }
  cout << ans.size() << endl;
  rep(i, 0, ans.size()) { cout << ans[i] << endl; }
}