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

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int x[4][30] = {{23, 1,  2,  26, 4,  5, 29, 7,  8, 20, 10, 11, 12, 13, 14,
                 17, 16, 15, 18, 19, 9, 21, 22, 0, 24, 25, 3,  27, 28, 6},
                {0,  1,  21, 3,  4,  24, 6, 7,  27, 9, 10, 18, 14, 13, 12,
                 15, 16, 17, 11, 19, 20, 2, 22, 23, 5, 25, 26, 8,  28, 29},
                {0,  1,  2,  3,  4,  5,  21, 22, 23, 11, 10, 9,  17, 13, 14,
                 15, 16, 12, 18, 19, 20, 6,  7,  8,  24, 25, 26, 27, 28, 29},
                {27, 28, 29, 3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 15,
                 14, 16, 17, 20, 19, 18, 21, 22, 23, 24, 25, 26, 0,  1,  2}};
vector<int> S = {1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4,
                 6, 6, 6, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3};
vector<int> D(30, 0);
map<vector<int>, int> M;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 30; j++)
      cin >> D[j];
    queue<pair<vector<int>, int>> Q;
    M.clear();
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(D, 0));
    M[D] = 1;
    if (D == S) {
      cout << "0" << endl;
      goto E;
    }
    while (!Q.empty()) {
      vector<int> A = Q.front().first;
      int AA = Q.front().second;
      Q.pop();
      for (int j = 0; j < 4; j++) {
        vector<int> B = A;
        for (int k = 0; k < 30; k++)
          B[x[j][k]] = A[k];
        if (B == S) {
          cout << AA + 1 << endl;
          goto E;
        }
        if (M[B] == 0) {
          Q.push(make_pair(B, AA + 1));
          M[B] = 1;
        }
      }
    }
  E:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int x[4][30] = {{23, 1,  2,  26, 4,  5, 29, 7,  8, 20, 10, 11, 12, 13, 14,
                 17, 16, 15, 18, 19, 9, 21, 22, 0, 24, 25, 3,  26, 27, 6},
                {0,  1,  21, 3,  4,  24, 6, 7,  27, 9, 10, 18, 14, 13, 12,
                 15, 16, 17, 11, 19, 20, 2, 22, 23, 5, 25, 26, 8,  28, 29},
                {0,  1,  2,  3,  4,  5,  22, 23, 24, 11, 10, 9,  17, 13, 14,
                 15, 16, 12, 18, 19, 20, 6,  7,  8,  24, 25, 26, 27, 28, 29},
                {27, 28, 29, 3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 15,
                 14, 16, 17, 20, 19, 18, 21, 22, 23, 24, 25, 26, 0,  1,  2}};
vector<int> S = {1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4,
                 6, 6, 6, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3};
vector<int> D(30, 0);
map<vector<int>, int> M;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 30; j++)
      cin >> D[j];
    queue<pair<vector<int>, int>> Q;
    M.clear();
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(D, 0));
    M[D] = 1;
    while (!Q.empty()) {
      vector<int> A = Q.front().first;
      int AA = Q.front().second;
      if (A == S) {
        cout << AA << endl;
        break;
      }
      Q.pop();
      for (int j = 0; j < 4; j++) {
        vector<int> B = A;
        for (int k = 0; k < 30; k++)
          B[x[j][k]] = A[k];
        if (M[B] == 0) {
          Q.push(make_pair(B, AA + 1));
          M[B] = 1;
        }
      }
    }
  }
  return 0;
}