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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

string list[24] = {"012345", "024135", "031425", "043215", "103254", "120534",
                   "135024", "152304", "201453", "215043", "240513", "254103",
                   "304152", "310542", "345012", "351402", "402351", "425031",
                   "430521", "453201", "513240", "521430", "534120", "542310"};

bool used[8];
vector<string> data(8);
vector<string> puzzle(8);

// TOP FRONT RIGHT LEFT BACK BOTTOM

int connect[8][8] = {
    {-1},       {0, 3, -1},       {1, 1, -1},       {0, 1, 2, 2, -1},
    {0, 5, -1}, {1, 5, 4, 3, -1}, {2, 5, 5, 1, -1}, {3, 5, 4, 1, 6, 2, -1}};
bool ok(char a, char b) { return a != b && (a & ~32) == (b & ~32); }

bool backTrack(int s) {
  if (s == 8) {
    // for(int i = 0 ; i < 8 ; i++)
    //	cout << "[" << puzzle[i].dice << "]"  << endl;
    return true;
  }
  for (int d = 0; d < 8; d++) {
    if (used[d] != 0)
      continue;
    used[d] = true;
    puzzle[s] = data[d];
    for (int i = 0; i < 24; i++) {
      for (int j = 0; j < 6; j++) {
        puzzle[s][j] = data[d][list[i][j]];
      }
      bool f = true;
      for (int j = 0; ~connect[s][j]; j += 2) {
        int pos = connect[s][j];
        int face = connect[s][j + 1];
        if (!ok(puzzle[pos][5 - face], puzzle[s][face])) {
          f = false;
        }
      }
      if (f)
        if (backTrack(s + 1))
          return true;
    }
    used[d] = false;
  }
  return false;
}
int main() {
  for (int i = 0; i < 24; i++)
    for (int j = 0; j < 6; j++)
      list[i][j] -= '0';
  while (1) {
    string str;
    for (int i = 0; i < 8; i++) {
      if (cin >> str, str == "0")
        return 0;
      data[i] = str;
      used[i] = 0;
    }

    cout << (backTrack(0) ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

class Die {
public:
  // TOP FRONT RIGHT LEFT BACK BOTTOM
  string dice;
  Die(string s) { dice = s; }
  Die() {}
  void left() {
    int to[6] = {1, 3, 4, 5, 2, 6};
    doRotate(to);
  }
  void north() {
    int to[6] = {2, 6, 3, 4, 1, 5};
    doRotate(to);
  }
  void west() {
    int to[6] = {3, 2, 6, 1, 5, 4};
    doRotate(to);
  }
  bool operator<(const Die &a) const { return this->dice < a.dice; }

private:
  void doRotate(int to[]) {
    char tmp[6];
    for (int i = 0; i < 6; i++)
      tmp[i] = dice[to[i] - 1];
    for (int i = 0; i < 6; i++)
      dice[i] = tmp[i];
  }
};

bool used[8];
vector<Die> data(8);
vector<Die> puzzle(8);

// TOP FRONT RIGHT LEFT BACK BOTTOM

int connect[8][8] = {
    {-1},       {0, 3, -1},       {1, 1, -1},       {0, 1, 2, 2, -1},
    {0, 5, -1}, {1, 5, 4, 3, -1}, {2, 5, 5, 1, -1}, {3, 5, 4, 1, 6, 2, -1}};
bool ok(char a, char b) { return a != b && (a & ~32) == (b & ~32); }

bool backTrack(int s) {
  if (s == 8) {
    // for(int i = 0 ; i < 8 ; i++)
    //	cout << "[" << puzzle[i].dice << "]"  << endl;
    return true;
  }
  for (int d = 0; d < 8; d++) {
    if (used[d] != 0)
      continue;
    used[d] = true;
    puzzle[s] = data[d];
    for (int x = 0; x < 2; x++) {
      for (int y = 0; y < 4; y++) {
        for (int z = 0; z < 4; z++) {
          bool f = true;
          for (int j = 0; ~connect[s][j]; j += 2) {
            int pos = connect[s][j];
            int face = connect[s][j + 1];
            if (!ok(puzzle[pos].dice[5 - face], puzzle[s].dice[face])) {
              f = false;
            }
          }
          if (f)
            if (backTrack(s + 1))
              return true;

          puzzle[s].north();
        }
        puzzle[s].west();
      }
      puzzle[s].left();
      puzzle[s].left();
    }
    used[d] = false;
  }
  return false;
}
int main() {
  while (1) {
    string str;
    for (int i = 0; i < 8; i++) {
      if (cin >> str, str == "0")
        return 0;
      data[i] = Die(str);
      used[i] = 0;
    }

    cout << (backTrack(0) ? "YES" : "NO") << endl;
  }
}