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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << (#a) << " " << (a) << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const int INF = (1 << 30) / 2;

int main() {
  int n;

  while (cin >> n && n) {
    vector<vint> v(2, vint(n));
    vector<vint> d((n + 1) * 2, vint((n + 1) * 2, INF));

    rep(i, 2) rep(j, n) cin >> v[i][j];

    rep(i, (n + 1)) d[i][i] = 0;

    // ツつサツづェツつシツづェツづ個湘ェツ渉環つゥツづァ1ツ偲ィツづ督楪達ツづつォツづゥツづつアツづォツづ可陛督づーツ陳」ツづゥ
    rep(y, n) rep(x, 2) {
      if (v[x][y] != 2) {
        for (int c = 0; c < 3 && y + c < n; c++) {
          int ny = y + c, nx = (x + 1) % 2;
          d[y * 2 + x][ny * 2 + nx] = 1;
        }

        if (v[x][y] == 1) {
          if (y + 1 < n && v[x][y + 1] == 1)
            d[y * 2 + x][(y + 1) * 2 + x] = 0;
        }
      } else {
        if (0 <= y - 1 && v[x][y - 1] == 2)
          d[y * 2 + x][(y - 1) * 2 + x] = 0;
      }
    }

    // ツ催終ツマツスツつゥツづァツ陛督づーツ陳」ツづゥ
    if (v[0][n - 1] != 2)
      d[(n - 1) * 2][n * 2 + 1] = 0;
    if (v[1][n - 1] != 2)
      d[(n - 1) * 2 + 1][n * 2] = 0;

    // ツδ渉ーツシツδδδ仰フツδ債イツドツづ催短ツ仰猟猟」ツづーツ出ツつキ
    rep(k, 2 * (n + 1)) rep(i, 2 * (n + 1)) rep(j, 2 * (n + 1))
        chmin(d[i][j], d[i][k] + d[k][j]);

    // ツづつソツづァツつゥツづァツづつソツづァツづ可行ツつュツづ個つェツ暗ェツ氾板短ツつ「ツつゥ
    int res = min(min(d[0][2 * n], d[0][2 * n + 1]),
                  min(d[1][2 * n], d[1][2 * n + 1]));

    if (res == INF)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const int INF = (1 << 30);
int main() {
  int n;

  while (cin >> n && n) {
    vector<vint> v(2, vint(n)), d(2, vint(n, INF));

    rep(i, 2) rep(j, n) cin >> v[i][j];

    if (v[0][n - 1] == 2 && v[1][n - 1] == 2) {
      cout << "NA" << endl;
      continue;
    }

    reverse(ALL(v[0]));
    reverse(ALL(v[1]));

    d[0][0] = d[1][0] = 0;
    bool update = false;

    do {
      update = false;
      rep(y, n) rep(x, 2) rep(c, 3) {
        int nx = (x + 1) % 2, ny = y + c;

        if (ny < n && d[x][y] != INF && d[x][y] + 1 < d[nx][ny] &&
            v[nx][ny] != 2) {
          d[nx][ny] = d[x][y] + 1;
          update = true;

          if (v[nx][ny] == 1) {
            for (int i = ny + 1; i < n && v[nx][i] == 1; i++) {
              if (d[nx][ny] < d[nx][i]) {
                d[nx][ny] = d[nx][i];
              }
            }
          }
        }
      }
    } while (update);

    int res = min(d[0][n - 1], d[1][n - 1]);
    if (res == INF)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}