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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int p[15], n, k, a1, a2, a3, a4, a5, power[16], dp[15000000];
queue<int> Q;
int main() {
  while (true) {
    cin >> n >> k;
    power[0] = 1;
    for (int i = 1; i < 16; i++)
      power[i] = power[i - 1] * 3;
    a5 = 0;
    if (n == 0 && k == 0)
      break;
    cin >> a1;
    for (int i = 0; i < a1; i++) {
      cin >> a4;
      p[a4 - 1] = 0;
    }
    cin >> a2;
    for (int i = 0; i < a2; i++) {
      cin >> a4;
      p[a4 - 1] = 1;
    }
    cin >> a3;
    for (int i = 0; i < a3; i++) {
      cin >> a4;
      p[a4 - 1] = 2;
    }
    for (int i = 0; i < n; i++) {
      a5 += power[i] * p[i];
    }
    for (int i = 0; i < power[n]; i++)
      dp[i] = 1000000000;
    dp[a5] = 0;
    Q.push(a5);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      vector<int> b[4];
      int C = 0;
      for (int i = n - 1; i >= 0; i--) {
        int p1 = (a1 / power[i]) % 3;
        b[p1 + 1].push_back(i);
        if (b[p1 + 1].size() == 1)
          C++;
        if (C == 3)
          break;
      }
      if (b[1].size() >= 1 && (b[2].size() == 0 || b[1][0] > b[2][0])) {
        int F = a1 + power[b[1][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[2].size() >= 1 && (b[1].size() == 0 || b[1][0] < b[2][0])) {
        int F = a1 - power[b[2][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[2].size() >= 1 && (b[3].size() == 0 || b[2][0] > b[3][0])) {
        int F = a1 + power[b[2][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[3].size() >= 1 && (b[2].size() == 0 || b[2][0] < b[3][0])) {
        int F = a1 - power[b[3][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
    }
    int ans = min(dp[0], dp[power[n] - 1]);
    if (ans > k)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int p[15], n, k, a1, a2, a3, a4, a5, power[16], dp[15000000];
queue<int> Q;
int main() {
  while (true) {
    cin >> n >> k;
    power[0] = 1;
    for (int i = 1; i < 16; i++)
      power[i] = power[i - 1] * 3;
    a5 = 0;
    if (n == 0 && k == 0)
      break;
    cin >> a1;
    for (int i = 0; i < a1; i++) {
      cin >> a4;
      p[a4 - 1] = 0;
    }
    cin >> a2;
    for (int i = 0; i < a2; i++) {
      cin >> a4;
      p[a4 - 1] = 1;
    }
    cin >> a3;
    for (int i = 0; i < a3; i++) {
      cin >> a4;
      p[a4 - 1] = 2;
    }
    for (int i = 0; i < n; i++) {
      a5 += power[i] * p[i];
    }
    for (int i = 0; i < power[n]; i++)
      dp[i] = 1000000000;
    dp[a5] = 0;
    Q.push(a5);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      vector<int> b[4];
      for (int i = n - 1; i >= 0; i--) {
        int p1 = (a1 / power[i]) % 3;
        b[p1 + 1].push_back(i);
      }
      if (b[1].size() >= 1 && (b[2].size() == 0 || b[1][0] > b[2][0])) {
        int F = a1 + power[b[1][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[2].size() >= 1 && (b[1].size() == 0 || b[1][0] < b[2][0])) {
        int F = a1 - power[b[2][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[2].size() >= 1 && (b[3].size() == 0 || b[2][0] > b[3][0])) {
        int F = a1 + power[b[2][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
      if (b[3].size() >= 1 && (b[2].size() == 0 || b[2][0] < b[3][0])) {
        int F = a1 - power[b[3][0]];
        if (dp[F] == 1000000000) {
          dp[F] = dp[a1] + 1;
          Q.push(F);
        }
      }
    }
    int ans = min(dp[0], dp[power[n] - 1]);
    if (ans > k)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}