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

#include <bits/stdc++.h>

using namespace std;

#define MAX 110
#define INF 1e9

struct State {
  int v, s;
  State(int v, int s) : v(v), s(s) {}
};

int N, W[2][MAX];

int bfs(int s) {
  queue<State> Q;
  Q.push(State(0, s));
  bool visited[2][MAX];
  memset(visited, false, sizeof(visited));
  int dist[2][MAX];
  fill(dist[0], dist[0] + MAX * 2, INF);
  dist[s][0] = 0;

  while (!Q.empty()) {
    int v = Q.front().v;
    int s = Q.front().s;
    Q.pop();

    if (visited[s][v])
      return INF;
    visited[s][v] = true;

    int cur = v;
    if (W[s][v] == 1) {
      for (int i = v + 1; i < N; i++) {
        if (W[s][i] != 1)
          break;
        dist[s][i] = min(dist[s][i], dist[s][v]);
        cur = i;
      }
    } else if (W[s][v] == 2) {
      for (int i = v - 1; i >= 0; i--) {
        cur = i;
        dist[s][i] = min(dist[s][i], dist[s][v]);
        if (W[s][i] != 2)
          break;
      }
    }

    if (cur == N - 1) {
      return dist[s][v];
    }
    v = cur;
    for (int i = 0; i < 3; i++) {
      if (dist[s][v] + 1 < dist[!s][v + i]) {
        dist[!s][v + i] = dist[s][v] + 1;
        Q.push(State(v + i, !s));
      }
    }
  }
  return INF;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < N; j++) {
        cin >> W[i][j];
      }
    }
    int res = min(bfs(0), bfs(1));
    if (res == INF) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 110
#define INF 1e9
typedef pair<int, int> pii;
typedef pair<int, bool> pii2;
typedef pair<pii, pii2> piii;

int N, A[MAX], B[MAX];

int dijkstra(int s) {
  priority_queue<piii, vector<piii>, greater<piii>> Q;
  Q.push(piii(pii(0, 0), pii2(s, false)));

  int dist[MAX][2];
  fill(dist[0], dist[0] + MAX * 2, INF);
  dist[0][s] = 0;

  while (!Q.empty()) {
    piii p = Q.top();
    Q.pop();
    int v = p.first.second;
    if (dist[v][p.second.first] < p.first.first)
      continue;

    if (p.second.first) {
      if (B[v] == 0 || p.second.second) {
        if (dist[v][1] + 1 < dist[v][0]) {
          dist[v][0] = dist[v][1] + 1;
          Q.push(piii(pii(dist[v][0], v), pii2(0, false)));
        }
        if (dist[v][1] + 1 < dist[v + 1][0]) {
          dist[v + 1][0] = dist[v][1] + 1;
          Q.push(piii(pii(dist[v + 1][0], v + 1), pii2(0, false)));
        }
        if (dist[v][1] + 1 < dist[v + 2][0]) {
          dist[v + 2][0] = dist[v][1] + 1;
          Q.push(piii(pii(dist[v + 2][0], v + 2), pii2(0, false)));
        }
      } else if (B[v] == 1) {
        int cur = v;
        for (int i = v; i < N; i++) {
          if (B[i] != 1)
            break;
          cur = i;
        }
        if (dist[v][1] < dist[cur][1]) {
          dist[cur][1] = dist[v][1];
          Q.push(piii(pii(dist[cur][1], cur), pii2(1, true)));
        }
      } else {
        int cur = v;
        for (int i = v; i >= 0; i--) {
          cur = i;
          if (B[i] != 2)
            break;
        }
        if (dist[v][1] < dist[cur][1]) {
          dist[cur][1] = dist[v][1];
          Q.push(piii(pii(dist[cur][1], cur), pii2(1, true)));
        }
      }
    } else {
      if (A[v] == 0 || p.second.second) {
        if (dist[v][0] + 1 < dist[v][1]) {
          dist[v][1] = dist[v][0] + 1;
          Q.push(piii(pii(dist[v][1], v), pii2(1, false)));
        }
        if (dist[v][0] + 1 < dist[v + 1][1]) {
          dist[v + 1][1] = dist[v][0] + 1;
          Q.push(piii(pii(dist[v + 1][1], v + 1), pii2(1, false)));
        }
        if (dist[v][0] + 1 < dist[v + 2][1]) {
          dist[v + 2][1] = dist[v][0] + 1;
          Q.push(piii(pii(dist[v + 2][1], v + 2), pii2(1, false)));
        }
      } else if (A[v] == 1) {
        int cur = v;
        for (int i = v; i < N; i++) {
          if (A[i] != 1)
            break;
          cur = i;
        }
        if (dist[v][0] < dist[cur][0]) {
          dist[cur][0] = dist[v][0];
          Q.push(piii(pii(dist[cur][0], cur), pii2(0, true)));
        }
      } else {
        int cur = v;
        for (int i = v; i >= 0; i--) {
          cur = i;
          if (A[i] != 2)
            break;
        }
        if (dist[v][0] < dist[cur][0]) {
          dist[cur][0] = dist[v][0];
          Q.push(piii(pii(dist[cur][0], cur), pii2(0, true)));
        }
      }
    }
  }
  int res = INF;
  for (int i = N; i < N + 2; i++) {
    for (int j = 0; j < 2; j++) {
      res = min(res, dist[i][j]);
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> A[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> B[i];
    }
    int res = min(dijkstra(0), dijkstra(1));
    if (res == INF) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}