Problem p00230 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> T;
#define INF (1 << 30)
int n, dist[2][210], window[2][210];
vector<T> edge[2][210];
bool come[2][210];
int main() {
  while (cin >> n, n) {
    for (int j = 0; j < 2; j++) {
      for (int i = 0; i < n; i++) {
        cin >> window[j][i];
        dist[j][i] = INF;
        come[j][i] = false;
        edge[j][i].clear();
      }
    }
    for (int j = 0; j < 2; j++) {
      for (int i = 0; i < n; i++) {
        if (window[j][i] == 0 || (window[j][i] == 1 && window[j][i + 1] != 1)) {
          for (int k = 0; k <= 2; k++) {
            edge[j][i].push_back(T(1, P(!j, i + k)));
          }
        }
        if (window[j][i + 1] == 1 && window[j][i] == 1) {
          edge[j][i].push_back(T(0, P(j, i + 1)));
        }
        if (window[j][i] == 2) {
          edge[j][i].push_back(T(0, P(j, i - 1)));
        }
      }
    }
    priority_queue<T, vector<T>, greater<T>> pq;
    pq.push(T(0, P(0, 0)));
    pq.push(T(0, P(1, 0)));
    dist[0][0] = dist[1][0] = 0;
    while (!pq.empty()) {
      T tmp = pq.top();
      pq.pop();
      int d = tmp.first, x = tmp.second.first, y = tmp.second.second;
      come[x][y] = true;
      if (dist[x][y] < d)
        continue;
      for (int i = 0; i < edge[x][y].size(); i++) {
        P to = edge[x][y][i].second;
        int r = edge[x][y][i].first, tx = to.first, ty = to.second;
        if (dist[tx][ty] <= d + r)
          continue;
        dist[tx][ty] = d + r;
        pq.push(T(dist[tx][ty], to));
      }
    }
    int res = min((window[0][n - 1] == 2) ? INF : dist[0][n - 1],
                  (window[1][n - 1] == 2) ? INF : dist[1][n - 1]);
    if (res == INF)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
#define INF (1 << 30)
int dp[2][210], n, w, window[2][210];
bool come[2][210], jump[2][210];

int solve(int x, int y) {
  if (y <= 0)
    return 0;
  int res = INF;
  if (come[x][y])
    return res;
  come[x][y] = true;
  for (int i = 0; i <= 2; i++) {
    if (y - i < 0)
      break;
    if (jump[!x][y - i])
      res = min(res, solve(!x, y - i) + 1);
  }
  if (y - 1 >= 0 && window[x][y] == 1 && window[x][y - 1] == 1)
    res = min(res, solve(x, y - 1));
  if (y + 1 < n && window[x][y + 1] == 2)
    res = min(res, solve(x, y + 1));
  come[x][y] = false;
  return res;
}

int main() {
  while (cin >> n, n) {
    for (int j = 0; j < 2; j++) {
      for (int i = 0; i < n; i++) {
        cin >> window[j][i];
        dp[j][i] = INF;
        jump[j][i] = false;
        if ((i > 0 && window[j][i - 1] == 0) ||
            (i > 0 && window[j][i] != 1 && window[j][i - 1] == 1))
          jump[j][i - 1] = true;
      }
      window[j][n] = 0;
      if ((window[j][n - 1] == 0) ||
          (window[j][n] != 1 && window[j][n - 1] == 1))
        jump[j][n - 1] = true;
    }

    int ans = min(solve(0, n), solve(1, n));
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}