Problem p00230 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n;
  while (cin >> n, n) {
    vvi in(n, vi(2));
    rep(i, 2) rep(j, n) cin >> in[j][i];
    vvi h(n, vi(2));
    int x = 0, y = 0;
    int out = 0, ans = inf;
    while (1) {
      if (x != n - 1)
        while (in[x][y] == 1 && in[x + 1][y] == 1) {
          x++;
          if (x == n - 1)
            break;
        }
      while (in[x][y] == 2)
        x--;
      if (x == n - 1)
        break;
      if (h[x][y] == 1)
        break;
      h[x][y] = true;
      out++;
      y ^= 1;
      x = min(x + 2, n - 1);
    }
    if (x == n - 1)
      ans = out;
    h = vvi(n, vi(2));
    x = 0;
    y = 1;
    out = 0;
    while (1) {
      if (x != n - 1)
        while (in[x][y] == 1 && in[x + 1][y] == 1) {
          x++;
          if (x == n - 1)
            break;
        }
      while (in[x][y] == 2)
        x--;
      if (x == n - 1)
        break;
      if (h[x][y] == 1)
        break;
      h[x][y] = true;
      out++;
      y ^= 1;
      x = min(x + 2, n - 1);
    }
    if (x == n - 1)
      cout << min(ans, out) << endl;
    else
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n;
  while (cin >> n, n) {
    vvi in(n, vi(2));
    rep(i, 2) rep(j, n) cin >> in[j][i];
    vvi h(n, vi(2));
    h[0][0] = 1;
    int x = 0, y = 0;
    int out = 0;
    while (1) {
      while (in[x][y] == 1 && in[x + 1][y] == 1) {
        x++;
        if (x == n - 1)
          break;
      }
      while (in[x][y] == 2)
        x--;
      if (x == n - 1)
        break;
      if (h[x][y] == 1)
        break;
      h[x][y] = true;
      out++;
      y ^= 1;
      x = min(x + 2, n - 1);
    }
    if (x == n - 1) {
      cout << out << endl;
      continue;
    }
    h = vvi(n, vi(2));
    h[0][1] = 1;
    x = 0;
    y = 1;
    out = 0;
    while (1) {
      while (in[x][y] == 1 && in[x + 1][y] == 1) {
        x++;
        if (x == n - 1)
          break;
      }
      while (in[x][y] == 2)
        x--;
      if (x == n - 1)
        break;
      if (h[x][y] == 1)
        break;
      h[x][y] = true;
      out++;
      y ^= 1;
      x = min(x + 2, n - 1);
    }
    if (x == n - 1)
      cout << out << endl;
    else
      cout << "NA" << endl;
  }
}