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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, L[120], R[120], LX[120], RX[120], LD[120], RD[120];
int INF = 999999;
int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++)
      cin >> L[i];
    for (int i = 0; i < n; i++)
      cin >> R[i];
    for (int i = 0; i < n; i++) {
      if (L[i] == 0) {
        LX[i] = i;
      }
      if (L[i] == 1) {
        for (int j = i; j < n; j++) {
          if (L[j] != 1) {
            break;
          }
          LX[i] = j;
        }
      }
      if (L[i] == 2) {
        for (int j = i; j >= 0; j--) {
          LX[i] = j;
          if (L[j] != 2) {
            break;
          }
        }
      }
      if (R[i] == 0) {
        RX[i] = i;
      }
      if (R[i] == 1) {
        for (int j = i; j < n; j++) {
          if (R[j] != 1) {
            break;
          }
          RX[i] = j;
        }
      }
      if (R[i] == 2) {
        for (int j = i; j >= 0; j--) {
          RX[i] = j;
          if (R[j] != 2) {
            break;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      LD[i] = INF;
      RD[i] = INF;
    }
    queue<pair<int, int>> Q;
    Q.push(make_pair(0, LX[0]));
    Q.push(make_pair(1, RX[0]));
    LD[LX[0]] = 0;
    RD[RX[0]] = 0;
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int a1 = pa.first, a2 = pa.second;
      if (a1 == 0) {
        if (RD[RX[a2]] == INF) {
          RD[RX[a2]] = LD[a2] + 1;
          Q.push(make_pair(1, RX[a2]));
        }
        if (RD[RX[a2 + 1]] == INF && a2 + 1 < n) {
          RD[RX[a2 + 1]] = LD[a2] + 1;
          Q.push(make_pair(1, RX[a2 + 1]));
        }
        if (RD[RX[a2 + 2]] == INF && a2 + 2 < n) {
          RD[RX[a2 + 2]] = LD[a2] + 1;
          Q.push(make_pair(1, RX[a2 + 2]));
        }
      }
      if (a1 == 1) {
        if (LD[LX[a2]] == INF) {
          LD[LX[a2]] = RD[a2] + 1;
          Q.push(make_pair(0, LX[a2]));
        }
        if (LD[LX[a2 + 1]] == INF && a2 + 1 < n) {
          LD[LX[a2 + 1]] = RD[a2] + 1;
          Q.push(make_pair(0, LX[a2 + 1]));
        }
        if (LD[LX[a2 + 2]] == INF && a2 + 2 < n) {
          LD[LX[a2 + 2]] = RD[a2] + 1;
          Q.push(make_pair(0, LX[a2 + 2]));
        }
      }
    }
    int V = min(LD[n - 1], RD[n - 1]);
    if (V == INF) {
      cout << "NA" << endl;
    } else {
      cout << V << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, L[120], R[120], LX[120], RX[120], LD[120], RD[120];
int INF = 999999;
int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++)
      cin >> L[i];
    for (int i = 0; i < n; i++)
      cin >> R[i];
    for (int i = 0; i < n; i++) {
      if (L[i] == 0) {
        LX[i] = 0;
      }
      if (L[i] == 1) {
        for (int j = i; j < n; j++) {
          if (L[j] != 1) {
            break;
          }
          LX[i] = j;
        }
      }
      if (L[i] == 2) {
        for (int j = i; j >= 0; j--) {
          LX[i] = j;
          if (LX[i] != 2) {
            break;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      LD[i] = INF;
      RD[i] = INF;
    }
    queue<pair<int, int>> Q;
    Q.push(make_pair(0, 0));
    Q.push(make_pair(1, 0));
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int a1 = pa.first, a2 = pa.second;
      if (a1 == 0) {
        if (RD[RX[a2]] == INF) {
          RD[RX[a2]] = LD[a2];
          Q.push(make_pair(1, RX[a2]));
        }
        if (RD[RX[a2 + 1]] == INF && a2 + 1 < n) {
          RD[RX[a2 + 1]] = LD[a2];
          Q.push(make_pair(1, RX[a2 + 1]));
        }
        if (RD[RX[a2 + 2]] == INF && a2 + 2 < n) {
          RD[RX[a2 + 2]] = LD[a2];
          Q.push(make_pair(1, RX[a2 + 2]));
        }
      }
      if (a1 == 1) {
        if (LD[RX[a2]] == INF) {
          LD[RX[a2]] = RD[a2];
          Q.push(make_pair(0, LX[a2]));
        }
        if (LD[RX[a2 + 1]] == INF && a2 + 1 < n) {
          LD[LX[a2 + 1]] = RD[a2];
          Q.push(make_pair(0, LX[a2 + 1]));
        }
        if (LD[RX[a2 + 2]] == INF && a2 + 2 < n) {
          LD[LX[a2 + 2]] = RD[a2];
          Q.push(make_pair(0, LX[a2 + 2]));
        }
      }
    }
    int V = min(LD[n - 1], RD[n - 1]);
    if (V == INF) {
      cout << "NA" << endl;
    } else {
      cout << V << endl;
    }
  }
}