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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define PB(a) push_back(a)
using namespace std;
typedef vector<int> vi;
int debu(string a) { cout << a << endl; }
int debu() { cout << "ok" << endl; }
int debu(int a) { cout << a << endl; }
int in() {
  int x;
  cin >> x;
  return x;
}
string kkk(string hoge, int a, int A, int b, int B, int c, int C, int d, int D,
           int e, int E) {
  swap(hoge[a], hoge[A]);
  swap(hoge[b], hoge[B]);
  swap(hoge[c], hoge[C]);
  swap(hoge[d], hoge[D]);
  swap(hoge[e], hoge[E]);
  return hoge;
}
bool kakunin(string a) {
  for (int i = 1; i <= 9; i++)
    if (a[i] != a[1] || a[i + 21] != a[22])
      return false;
  for (int i = 1; i <= 3; i++)
    if (a[9 + i] != a[10] || a[i + 12] != a[13] || a[i + 15] != a[16] ||
        a[i + 18] != a[19])
      return false;
  return true;
}
int main() {
  int n;
  cin >> n;
  rep(hoge, n) {
    string a = "a";
    rep(i, 30) {
      char f;
      cin >> f;
      a += f;
    }

    map<string, int> mp;
    mp[a] = 0;
    if (kakunin(a)) {
      cout << 0 << endl;
      continue;
    }
    queue<string> qu;
    qu.push(a);

    while (!qu.empty()) {
      string now = qu.front();
      qu.pop();
      string b;

      b = kkk(now, 1, 28, 2, 29, 3, 30, 19, 21, 15, 16);
      if (mp.count(b) == 0) {
        mp[b] = mp[now] + 1;
        qu.push(b);
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = kkk(now, 7, 22, 8, 23, 9, 24, 13, 18, 10, 12);
      if (mp.count(b) == 0) {
        mp[b] = mp[now] + 1;
        qu.push(b);
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = kkk(now, 1, 24, 4, 27, 7, 30, 10, 21, 16, 18);
      if (mp.count(b) == 0) {
        mp[b] = mp[now] + 1;
        qu.push(b);
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = kkk(now, 3, 22, 6, 25, 9, 28, 12, 19, 13, 15);
      if (mp.count(b) == 0) {
        mp[b] = mp[now] + 1;
        qu.push(b);
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define PB(a) push_back(a)
#define sp(i, l) swap(b[i], b[l])
using namespace std;
typedef vector<int> vi;
int debu(string a) { cout << a << endl; }
int debu() { cout << "ok" << endl; }
int debu(int a) { cout << a << endl; }
int in() {
  int x;
  cin >> x;
  return x;
}
int kakunin(string a) {
  for (int i = 1; i <= 9; i++) {
    if (a[i] != a[1])
      return 0;
    if (a[i + 21] != a[28])
      return 0;
  }
  for (int i = 1; i <= 3; i++) {
    if (a[i + 12] != a[13])
      return 0;
    if (a[i + 9] != a[10])
      return 0;
    if (a[i + 18] != a[19])
      return 0;
    if (a[i + 15] != a[16])
      return 0;
  }
  return 1;
}

int main() {
  int n;
  cin >> n;
  rep(fh, n) {
    string a = "a";

    rep(i, 30) {
      char z;
      cin >> z;
      a += z;
    }
    if (kakunin(a)) {
      cout << 0 << endl;
      continue;
    }
    map<string, int> mp;
    queue<string> q;
    q.push(a);
    mp[a] = 0;
    while (!q.empty()) {
      string now = q.front();
      q.pop();

      string b = now;
      for (int i = 1; i <= 3; i++) {
        sp(i, 31 - i);
      }
      sp(15, 16);
      sp(21, 19);
      if (!mp.count(b) || mp[b] < mp[now]) {
        q.push(b);
        mp[b] = mp[now] + 1;
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = now;
      for (int i = 1; i <= 7; i += 3) {
        sp(i, 23 + i);
      }
      sp(18, 16);
      sp(21, 10);
      if (!mp.count(b) || mp[b] < mp[now]) {
        q.push(b);
        mp[b] = mp[now] + 1;
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = now;
      for (int i = 1; i <= 3; i++) {
        sp(i + 6, 25 - i);
      }
      sp(13, 18);
      sp(10, 12);
      if (!mp.count(b) || mp[b] < mp[now]) {
        q.push(b);
        mp[b] = mp[now] + 1;
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }

      b = now;
      for (int i = 3; i <= 9; i += 3) {
        sp(i, 19 + i);
      }
      sp(13, 15);
      sp(12, 19);
      if (!mp.count(b) || mp[b] < mp[now]) {
        q.push(b);
        mp[b] = mp[now] + 1;
        if (kakunin(b)) {
          cout << mp[b] << endl;
          break;
        }
      }
    }
  }
}