Problem p03263 (C++)

Buggy submission (status: Runtime Error):
// abc109_d.cpp
// Sun Apr 21 19:49:14 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int h, w;
  cin >> h >> w;

  vector<P> oddpair;
  int a[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  int dir = -1;
  for (int i = 0; i < h; i++) {
    dir = dir * (-1);
    if (dir == 1) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] % 2 == 1) {
          P temp;
          temp.first = i;
          temp.second = j;
          oddpair.push_back(temp);
        }
      }
    } else {
      for (int j = w - 1; j >= 0; j--) {
        if (a[i][j] % 2 == 1) {
          P temp;
          temp.first = i;
          temp.second = j;
          oddpair.push_back(temp);
        }
      }
    }
  }

  int count = 0;
  int y1[500];
  int x1[500];
  int y1d[500];
  int x1d[500];
  for (int i = 0; i < oddpair.size(); i++) {
    if (i == oddpair.size() - 1) {
      break;
    }
    P fromp = oddpair[i];
    i++;
    P top = oddpair[i];
    //		cout << fromp.first << " " << fromp.second << " - " << top.first << "
    //" << top.second << endl;
    int dir = 0;
    if (fromp.first % 2 == 0) {
      dir = 1;
    } else {
      dir = -1;
    }
    while (fromp.first != top.first || fromp.second != top.second) {
      //			cout << "pass" << endl;
      if (fromp.second == 0 && dir == -1) {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first + 1;
        x1d[count] = fromp.second;
        fromp.first++;
        dir = 1;
      } else if (fromp.second == w - 1 && dir == 1) {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first + 1;
        x1d[count] = fromp.second;
        fromp.first++;
        dir = -1;
      } else {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first;
        x1d[count] = fromp.second + dir;
        fromp.second += dir;
      }
      count++;
    }
  }

  cout << count << endl;
  for (int i = 0; i < count; i++) {
    cout << y1[i] + 1 << " " << x1[i] + 1 << " " << y1d[i] + 1 << " "
         << x1d[i] + 1 << endl;
  }
}

Fixed submission:
// abc109_d.cpp
// Sun Apr 21 19:49:14 2019

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int h, w;
  cin >> h >> w;

  vector<P> oddpair;
  int a[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  int dir = -1;
  for (int i = 0; i < h; i++) {
    dir = dir * (-1);
    if (dir == 1) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] % 2 == 1) {
          P temp;
          temp.first = i;
          temp.second = j;
          oddpair.push_back(temp);
        }
      }
    } else {
      for (int j = w - 1; j >= 0; j--) {
        if (a[i][j] % 2 == 1) {
          P temp;
          temp.first = i;
          temp.second = j;
          oddpair.push_back(temp);
        }
      }
    }
  }

  int count = 0;
  int y1[250000];
  int x1[250000];
  int y1d[250000];
  int x1d[250000];
  for (int i = 0; i < oddpair.size(); i++) {
    if (i == oddpair.size() - 1) {
      break;
    }
    P fromp = oddpair[i];
    i++;
    P top = oddpair[i];
    //		cout << fromp.first << " " << fromp.second << " - " << top.first << "
    //" << top.second << endl;
    int dir = 0;
    if (fromp.first % 2 == 0) {
      dir = 1;
    } else {
      dir = -1;
    }
    while (fromp.first != top.first || fromp.second != top.second) {
      //			cout << "pass" << endl;
      if (fromp.second == 0 && dir == -1) {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first + 1;
        x1d[count] = fromp.second;
        fromp.first++;
        dir = 1;
      } else if (fromp.second == w - 1 && dir == 1) {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first + 1;
        x1d[count] = fromp.second;
        fromp.first++;
        dir = -1;
      } else {
        y1[count] = fromp.first;
        x1[count] = fromp.second;
        y1d[count] = fromp.first;
        x1d[count] = fromp.second + dir;
        fromp.second += dir;
      }
      count++;
    }
  }

  cout << count << endl;
  for (int i = 0; i < count; i++) {
    cout << y1[i] + 1 << " " << x1[i] + 1 << " " << y1d[i] + 1 << " "
         << x1d[i] + 1 << endl;
  }
}