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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
int dx[] = {0, 1, 2};
int N;

void ladder(VVI &tower, P &p) {
  while (p.F < N && tower[p.S][p.F] == 1)
    p.F++;
  p.F--;
}

void slip(VVI &tower, P &p) {
  while (tower[p.S][p.F] == 2 && p.F > 0)
    p.F--;
}

int main() {

  while (cin >> N, N) {
    vector<vector<int>> tower, mincost;
    tower.resize(2), mincost.resize(2);
    tower[0].resize(N + 1), tower[1].resize(N + 1);
    mincost[0].resize(N + 1), mincost[1].resize(N + 1);

    for (int j = 0; j < 2; j++)
      for (int i = 0; i < N; i++)
        cin >> tower[j][i], mincost[j][i] = (1 << 28);

    mincost[0][0] = mincost[1][0] = 0;
    deque<P> deq;
    P fst = P(0, 0);
    if (tower[fst.S][fst.F] == 1)
      ladder(tower, fst);
    deq.push_back(fst); // false が左
    mincost[fst.S][fst.F] = 0;
    fst = P(0, 1);
    if (tower[fst.S][fst.F] == 1)
      ladder(tower, fst);
    deq.push_back(fst); // true  が右
    mincost[fst.S][fst.F] = 0;

    while (!deq.empty()) {
      P p = deq.front();
      deq.pop_front();
      P pre = p;
      if (p.F >= N)
        continue;

      p.S = !p.S;
      for (int i = 0; i < 3; i++) {
        int nx = p.F + dx[i];
        if (nx >= N)
          nx = N - 1;
        P next = P(nx, p.S);
        if (tower[p.S][nx] == 2)
          slip(tower, next);
        if (tower[next.S][next.F] == 1)
          ladder(tower, next);

        if (mincost[next.S][next.F] > mincost[pre.S][pre.F] + 1) {
          mincost[next.S][next.F] = mincost[pre.S][pre.F] + 1;
          deq.push_back(next);
        }
      }
    }

    if (mincost[0][N - 1] != (1 << 28) || mincost[1][N - 1] != (1 << 28))
      cout << min(mincost[0][N - 1], mincost[1][N - 1]) << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(), n.end()
#define pb push_back
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define INF (1 << 27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dir[3] = {0, 1, 2};
const int DRAW = 100000;

class AOJ0230 {
private:
  int n;
  int **imap;
  int d[110][2];

public:
  AOJ0230(int n, int **F) : n(n) {
    imap = new int *[n + 1];
    rep(i, n) imap[i] = new int[2];

    rep(i, n) imap[i][0] = *(*(F + i));
    rep(i, n) * (*(imap + i) + 1) = *(*(F + i) + 1);
    rep(i, n) {
      // if(imap[i][0] == 1)d[i][0] = -1,d[i][1] = DRAW;
      // else if(imap[i][1] == 1)d[i][1] = -1,d[i][0] = DRAW;
      // else if(imap[i][0] == 2)d[i][0] = -1,d[i][1] = DRAW;
      // else if(imap[i][1] == 2)d[i][1] = -1,d[i][0] = DRAW;
      /*else*/ d[i][0] = d[i][1] = DRAW;
    }
  }

  ~AOJ0230() {
    delete[] imap[0];
    delete[] imap;
  }

  int getPos(int np, int lorr) {
    // cout << "GET! np = "<< np << ", lorr = " << lorr << ", mpa = " <<
    // imap[np][lorr] << endl;
    if (imap[np][lorr] == 1) {
      for (int i = np; i < n && imap[i][lorr] == 1; i++, np++) {
      }
      if (np > n - 1)
        np = n - 1;
    } else if (imap[np][lorr] == 2) {
      for (int i = np; i >= 0 && imap[i][lorr] == 2; i--, np--) {
      }
      // cout << "np = " << np << endl;
      if (np < 0)
        np = 0;
      else if (imap[np][lorr] == 1)
        np++;
    }

    if (np > n - 1)
      return -1;
    else
      return np;
  }

  void run() {
    queue<P> que;
    que.push(P(0, 0)); // P(L or R,pos) L or R: 0 -> left, 1 -> right
    que.push(P(1, 0));
    d[0][0] = d[0][1] = 1;

    bool arrived = false;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      // cout << "p.info = " << p.F << "," << p.S << endl;
      assert(p.S <= n - 1);
      if (p.S == n - 1) {
        int lorr = p.F == 1 ? 0 : 1;
        // cout << "GET ANSWER!!! " << d[n-1][lorr] << endl;
        arrived = true;
        continue;
      }

      int lorr = p.F == 1 ? 0 : 1;

      rep(i, 3) {
        if (p.S + dir[i] > n - 1)
          continue;
        // cout << "pre-position = "<< p.S << " to ";
        int np = getPos(p.S + dir[i], lorr);
        // cout  << np << ", d["<<np<<"]["<<lorr<<"] = " << d[np][lorr] <<
        // ",pre-d["<<p.S<<"]["<<p.F<<"] = "<< d[p.S][p.F] << endl << endl;

        if (np == -1)
          continue;
        if (d[np][lorr] < d[p.S][p.F] + 1)
          continue;

        d[np][lorr] = d[p.S][p.F] + 1;
        // cout << "ARRIVED! d[" << np << "][" << lorr <<"] = " << d[np][lorr]
        // << endl << endl;
        que.push(P(lorr, np));
      }
    }
    if (!arrived)
      cout << "NA" << endl;
    else
      cout << min(d[n - 1][0], d[n - 1][1]) << endl;
  }

  void print() {
    cout << endl << "left - imap ###" << endl;
    rep(i, n) cout << *(*(imap + i)) << ' ';
    cout << endl;

    cout << "right - imap ###" << endl;
    rep(i, n) cout << *(*(imap + i) + 1) << ' ';
    cout << endl;
  }
};

int main() {
  int N;
  while (cin >> N && N) {
    int **inputs;
    inputs = new int *[N];
    rep(i, N) inputs[i] = new int[2];
    rep(i, N) cin >> inputs[i][0];
    rep(i, N) cin >> inputs[i][1];
    cerr << N << endl;
    rep(i, N) cerr << inputs[i][0] << ' ';
    cerr << endl;
    rep(i, N) cerr << inputs[i][1] << ' ';
    cerr << endl;
    AOJ0230 ans = AOJ0230(N, inputs);
    delete[] inputs[0];
    delete[] inputs;
    cout << 4 << endl;
    // cout << ans.getPos(2,1) << endl;
    // ans.run();
  }
  return 0;
}