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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int match(string s) {
  if (s[0] != s[1] || s[1] != s[2] || s[2] != s[3] || s[3] != s[4] ||
      s[4] != s[5] || s[5] != s[6] || s[6] != s[7] || s[7] != s[8])
    return 0;
  if (s[9] != s[10] || s[10] != s[11])
    return 0;
  if (s[12] != s[13] || s[13] != s[14])
    return 0;
  if (s[15] != s[16] || s[16] != s[17])
    return 0;
  if (s[18] != s[19] || s[19] != s[20])
    return 0;
  if (s[21] != s[22] || s[22] != s[23] || s[23] != s[24] || s[24] != s[25] ||
      s[25] != s[26] || s[26] != s[27] || s[27] != s[28] || s[28] != s[29])
    return 0;

  return 1;
}

string lswap(string s) {
  swap(s[0], s[27]);
  swap(s[1], s[28]);
  swap(s[2], s[29]);
  swap(s[14], s[15]);
  swap(s[18], s[20]);
  return s;
}

string rswap(string s) {
  swap(s[6], s[21]);
  swap(s[7], s[22]);
  swap(s[8], s[23]);
  swap(s[12], s[17]);
  swap(s[9], s[11]);
  return s;
}

string uswap(string s) {
  swap(s[2], s[21]);
  swap(s[5], s[24]);
  swap(s[8], s[27]);
  swap(s[11], s[18]);
  swap(s[12], s[14]);
  return s;
}

string sswap(string s) {
  swap(s[0], s[23]);
  swap(s[3], s[26]);
  swap(s[6], s[29]);
  swap(s[9], s[20]);
  swap(s[15], s[17]);
  return s;
}

int solve(string s, int c, int i) {
  if (match(s) || c >= 8)
    return c;
  int a = 8, b = 8, d = 8, e = 8;
  if (i != 1)
    a = solve(lswap(s), c + 1, 1);
  if (i != 2)
    b = solve(rswap(s), c + 1, 2);
  if (i != 3)
    d = solve(uswap(s), c + 1, 3);
  if (i != 4)
    e = solve(sswap(s), c + 1, 4);

  return min(min(a, b), min(d, e));
}

int main() {
  int n;
  cin >> n;

  rep(i, 0, n) {
    string a, s = "";
    rep(i, 0, 30) {
      cin >> a;
      s += a;
    }
    cout << solve(s, 0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int match(string s) {
  if (s[0] != s[1] || s[1] != s[2] || s[2] != s[3] || s[3] != s[4] ||
      s[4] != s[5] || s[5] != s[6] || s[6] != s[7] || s[7] != s[8])
    return 0;
  if (s[9] != s[10] || s[10] != s[11])
    return 0;
  if (s[12] != s[13] || s[13] != s[14])
    return 0;
  if (s[15] != s[16] || s[16] != s[17])
    return 0;
  if (s[18] != s[19] || s[19] != s[20])
    return 0;
  if (s[21] != s[22] || s[22] != s[23] || s[23] != s[24] || s[24] != s[25] ||
      s[25] != s[26] || s[26] != s[27] || s[27] != s[28] || s[28] != s[29])
    return 0;

  return 1;
}

string lswap(string s) {
  swap(s[0], s[29]);
  swap(s[1], s[28]);
  swap(s[2], s[27]);
  swap(s[14], s[15]);
  swap(s[18], s[20]);
  return s;
}

string rswap(string s) {
  swap(s[6], s[23]);
  swap(s[7], s[22]);
  swap(s[8], s[21]);
  swap(s[12], s[17]);
  swap(s[9], s[11]);
  return s;
}

string uswap(string s) {
  swap(s[2], s[21]);
  swap(s[5], s[24]);
  swap(s[8], s[27]);
  swap(s[11], s[18]);
  swap(s[12], s[14]);
  return s;
}

string sswap(string s) {
  swap(s[0], s[23]);
  swap(s[3], s[26]);
  swap(s[6], s[29]);
  swap(s[9], s[20]);
  swap(s[15], s[17]);
  return s;
}

int solve(string s, int c) {
  if (match(s) || c > 8)
    return c;

  int a = solve(lswap(s), c + 1);
  int b = solve(rswap(s), c + 1);
  int d = solve(uswap(s), c + 1);
  int e = solve(sswap(s), c + 1);

  return min(min(a, b), min(d, e));
}

int main() {
  int n;
  cin >> n;

  rep(i, 0, n) {
    string a, s = "";
    rep(i, 0, 30) {
      cin >> a;
      s += a;
    }
    cout << solve(s, 0) << endl;
  }
}