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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// C++11
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
string piece[9];
int rot[9];
int id[9];
int bgn[10] = {0, 0, 1, 2, 3, 5, 7, 8, 10, 12};
int check[12][4] = {{1, 3, 0, 1}, {2, 3, 1, 1}, {3, 0, 0, 2}, {4, 0, 1, 2},
                    {4, 3, 3, 1}, {5, 0, 2, 2}, {5, 3, 4, 1}, {6, 0, 3, 2},
                    {7, 0, 4, 2}, {7, 3, 6, 1}, {8, 0, 5, 2}, {8, 3, 7, 1}};
inline bool ok(char c, char d) { return c != d && tolower(c) == tolower(d); }
int ans;
void dfs(int u, int S) {
  if (u == 9) {
    ans++;
    return;
  }
  for (int nu = 0; nu < 9; nu++)
    if (!(S >> nu & 1)) {
      id[u] = nu;
      int NS = S | (1 << nu);
      for (int r = 0; r <= 3; r++) {
        rot[u] = r;
        bool next = true;
        for (int i = bgn[u]; i < bgn[u + 1]; i++) {
          int p = check[i][0], q = check[i][2];
          int c1 = (check[i][1] + rot[p]) % 4, c2 = (check[i][3] + rot[q]) % 4;
          if (!ok(piece[id[p]][c1], piece[id[q]][c2]))
            next = false;
        }
        if (next)
          dfs(u + 1, NS);
      }
    }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    REP(i, 9) cin >> piece[i];
    sort(piece, piece + 9);
    ans = 0;
    dfs(0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// C++11
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
string piece[9];
int rot[9];
int bgn[11] = {0, 0, 0, 1, 2, 3, 5, 7, 8, 10, 12};
int check[12][4] = {{1, 3, 0, 1}, {2, 3, 1, 1}, {3, 0, 0, 2}, {4, 0, 1, 2},
                    {4, 3, 3, 1}, {5, 0, 2, 2}, {5, 3, 4, 1}, {6, 0, 3, 2},
                    {7, 0, 4, 2}, {7, 3, 6, 1}, {8, 0, 5, 2}, {8, 3, 7, 1}};
inline bool ok(char c, char d) { return c != d && tolower(c) == tolower(d); }
int ans;
void dfs(int u) {
  for (int i = bgn[u]; i < bgn[u + 1]; i++) {
    int p = check[i][0], q = check[i][2];
    int c1 = (check[i][1] + rot[p]) % 4, c2 = (check[i][3] + rot[q]) % 4;
    if (!ok(piece[p][c1], piece[q][c2]))
      return;
  }
  if (u == 9) {
    ans++;
    return;
  }
  for (int i = 0; i <= 3; i++) {
    rot[u] = i;
    dfs(u + 1);
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    REP(i, 9) cin >> piece[i];
    sort(piece, piece + 9);
    ans = 0;
    do {
      dfs(0);
    } while (next_permutation(piece, piece + 9));
    cout << ans << endl;
  }
  return 0;
}