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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int bfs[15000000], power[20], n, K, b, c, a[15];
queue<vector<int>> Q;
int hush(vector<int> v) {
  int p = 0;
  for (int i = 0; i < v.size(); i++)
    p += power[i] * v[i];
  return p;
}
int main() {
  power[0] = 1;
  for (int i = 1; i < 20; i++)
    power[i] = power[i - 1] * 3;
  while (true) {
    for (int i = 0; i < 15000000; i++)
      bfs[i] = 1000000000;
    cin >> n >> K;
    vector<int> s;
    if (n == 0 && K == 0)
      break;
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 0;
    }
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 1;
    }
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 2;
    }
    for (int i = 0; i < n; i++)
      s.push_back(a[i]);
    bfs[hush(s)] = 0;
    Q.push(s);
    while (!Q.empty()) {
      vector<int> a1, a2, a3, t = Q.front();
      Q.pop();
      int V = hush(t);
      int A1 = -1, A2 = -1, A3 = -1;
      for (int i = 0; i < n; i++) {
        if (t[i] == 0)
          A1 = i;
        if (t[i] == 1)
          A2 = i;
        if (t[i] == 2)
          A3 = i;
      }
      if (A1 >= 0 && A2 <= A1) {
        int P1 = hush(t) + power[A1];
        if (bfs[P1] == 1000000000) {
          t[A1] = 1;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A1] = 0;
        }
      }
      if (A2 >= 0 && A1 <= A2) {
        int P1 = hush(t) - power[A2];
        if (bfs[P1] == 1000000000) {
          t[A2] = 0;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A2] = 1;
        }
      }
      if (A2 >= 0 && A3 <= A2) {
        int P1 = hush(t) + power[A2];
        if (bfs[P1] == 1000000000) {
          t[A2] = 2;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A2] = 1;
        }
      }
      if (A3 >= 0 && A2 <= A3) {
        int P1 = hush(t) - power[A3];
        if (bfs[P1] == 1000000000) {
          t[A3] = 1;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A3] = 2;
        }
      }
    }
    int L = min(bfs[0], bfs[power[n] - 1]);
    if (L > K)
      L = -1;
    cout << L << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int bfs[15000000], power[20], n, K, b, c, a[15];
queue<vector<int>> Q;
int hush(vector<int> v) {
  int p = 0;
  for (int i = 0; i < v.size(); i++)
    p += power[i] * v[i];
  return p;
}
int main() {
  power[0] = 1;
  for (int i = 1; i < 20; i++)
    power[i] = power[i - 1] * 3;
  while (true) {
    for (int i = 0; i < 15000000; i++)
      bfs[i] = 1000000000;
    cin >> n >> K;
    vector<int> s;
    if (n == 0 && K == 0)
      break;
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 0;
    }
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 1;
    }
    cin >> b;
    for (int i = 0; i < b; i++) {
      cin >> c;
      a[c - 1] = 2;
    }
    for (int i = 0; i < n; i++)
      s.push_back(a[i]);
    bfs[hush(s)] = 0;
    Q.push(s);
    while (!Q.empty()) {
      vector<int> a1, a2, a3, t = Q.front();
      Q.pop();
      int V = hush(t);
      for (int i = 0; i < n; i++) {
        if (t[i] == 0)
          a1.push_back(i);
        if (t[i] == 1)
          a2.push_back(i);
        if (t[i] == 2)
          a3.push_back(i);
      }
      int A1 = -1, A2 = -1, A3 = -1;
      if (a1.size() >= 1)
        A1 = a1[a1.size() - 1];
      if (a2.size() >= 1)
        A2 = a2[a2.size() - 1];
      if (a3.size() >= 1)
        A3 = a3[a3.size() - 1];
      if (A1 >= 0 && A2 <= A1) {
        int P1 = hush(t) + power[A1];
        if (bfs[P1] == 1000000000) {
          t[A1] = 1;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A1] = 0;
        }
      }
      if (A2 >= 0 && A1 <= A2) {
        int P1 = hush(t) - power[A2];
        if (bfs[P1] == 1000000000) {
          t[A2] = 0;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A2] = 1;
        }
      }
      if (A2 >= 0 && A3 <= A2) {
        int P1 = hush(t) + power[A2];
        if (bfs[P1] == 1000000000) {
          t[A2] = 2;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A2] = 1;
        }
      }
      if (A3 >= 0 && A2 <= A3) {
        int P1 = hush(t) - power[A3];
        if (bfs[P1] == 1000000000) {
          t[A3] = 1;
          bfs[P1] = bfs[V] + 1;
          Q.push(t);
          t[A3] = 2;
        }
      }
    }
    int L = min(bfs[0], bfs[power[n] - 1]);
    if (L > K)
      L = -1;
    cout << L << endl;
  }
  return 0;
}