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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

string dice[8];
string rotdice[8][24];
string sdice[8];
bool used[8];

bool val(int pos) {
  int x = pos % 2, y = pos / 2 % 2, z = pos / 4 % 2;
  // if(x==1)cout<<pos<<" "<<sdice[pos]<<" "<<sdice[pos-1]<<"
  // "<<abs(sdice[pos][3]-sdice[pos-1][2])<<endl; if(y==1)cout<<pos<<"
  // "<<sdice[pos]<<" "<<sdice[pos-2]<<"
  // "<<abs(sdice[pos][4]-sdice[pos-2][1])<<endl; if(z==1)cout<<pos<<"
  // "<<sdice[pos]<<" "<<sdice[pos-4]<<"
  // "<<abs(sdice[pos][5]-sdice[pos-4][0])<<endl;
  if (x == 1 && abs(sdice[pos][3] - sdice[pos - 1][2]) != 32)
    return false;
  if (y == 1 && abs(sdice[pos][4] - sdice[pos - 2][1]) != 32)
    return false;
  if (z == 1 && abs(sdice[pos][5] - sdice[pos - 4][0]) != 32)
    return false;
  return true;
}

string h(string s, int n) {
  if (n == 0)
    return s;
  string ss;
  ss += s[0];
  ss += s[3];
  ss += s[1];
  ss += s[4];
  ss += s[2];
  ss += s[5];
  return h(ss, n - 1);
}

string g(string s, int n) {
  int pos[6][6] = {
      {0, 1, 2, 3, 4, 5}, {1, 5, 2, 3, 0, 4}, {2, 1, 5, 0, 4, 3},
      {3, 1, 0, 5, 4, 2}, {4, 0, 2, 3, 5, 1}, {5, 4, 2, 3, 1, 0},
  };
  string res;
  rep(i, 6) res += s[pos[n][i]];
  return res;
}

bool f(int pos) {
  if (pos == 8) {
    return true;
  }
  rep(k, 8) {
    if (pos == 0 && k > 0)
      break;
    if (used[k])
      continue;
    used[k] = true;
    rep(i, 24) {
      sdice[pos] = rotdice[k][i];
      if (!val(pos))
        continue;
      if (f(pos + 1))
        return true;
    }
    used[k] = false;
  }
  return false;
}

int main() {
  IL {
    cin >> dice[0];
    if (dice[0] == "0")
      break;
    rep(i, 7) cin >> dice[i + 1];
    rep(i, 8) used[i] = false;
    rep(k, 8) {
      rep(i, 6) {
        string d = g(dice[k], i);
        rep(j, 4) { rotdice[k][i * 4 + j] = h(d, j); }
      }
    }
    if (f(0)) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;
typedef stringstream SS;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
SS ss;
#define debug ss
#endif

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repnd(i, m, n) for (int i = (n); i >= (m); --i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; --i)
#define repnd0(i, m, n) for (i = (n); i >= (m); --i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

string dice[8];
string sdice[8];
bool used[8];

bool val(int pos) {
  int x = pos % 2, y = pos / 2 % 2, z = pos / 4 % 2;
  // if(x==1)cout<<pos<<" "<<sdice[pos]<<" "<<sdice[pos-1]<<"
  // "<<abs(sdice[pos][3]-sdice[pos-1][2])<<endl; if(y==1)cout<<pos<<"
  // "<<sdice[pos]<<" "<<sdice[pos-2]<<"
  // "<<abs(sdice[pos][4]-sdice[pos-2][1])<<endl; if(z==1)cout<<pos<<"
  // "<<sdice[pos]<<" "<<sdice[pos-4]<<"
  // "<<abs(sdice[pos][5]-sdice[pos-4][0])<<endl;
  if (x == 1 && abs(sdice[pos][3] - sdice[pos - 1][2]) != 32)
    return false;
  if (y == 1 && abs(sdice[pos][4] - sdice[pos - 2][1]) != 32)
    return false;
  if (z == 1 && abs(sdice[pos][5] - sdice[pos - 4][0]) != 32)
    return false;
  return true;
}

string h(string s, int n) {
  if (n == 0)
    return s;
  string ss;
  ss += s[0];
  ss += s[3];
  ss += s[1];
  ss += s[4];
  ss += s[2];
  ss += s[5];
  return h(ss, n - 1);
}

string g(string s, int n) {
  int pos[6][6] = {
      {0, 1, 2, 3, 4, 5}, {1, 5, 2, 3, 0, 4}, {2, 1, 5, 0, 4, 3},
      {3, 1, 0, 5, 4, 2}, {4, 0, 2, 3, 5, 1}, {5, 4, 2, 3, 1, 0},
  };
  string res;
  rep(i, 6) res += s[pos[n][i]];
  return res;
}

bool f(int pos) {
  if (pos == 8) {
    return true;
  }
  rep(k, 8) {
    if (pos == 0 && k > 0)
      break;
    if (used[k])
      continue;
    used[k] = true;
    string ddd = dice[k];
    rep(i, 6) {
      string d = g(ddd, i);
      rep(j, 4) {
        sdice[pos] = h(d, j);
        if (!val(pos))
          continue;
        if (f(pos + 1))
          return true;
      }
    }
    used[k] = false;
  }
  return false;
}

int main() {
  IL {
    cin >> dice[0];
    if (dice[0] == "0")
      break;
    rep(i, 7) cin >> dice[i + 1];
    rep(i, 8) used[i] = false;
    if (f(0)) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}