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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

// 24ñÅ³ÉàÇé
/*
zñÆ_CXÌÊÌÖW
c0ÍRXgN^ÌøÅ^¦çêéxN^Ìvf
0 c4 0 0
c3 c0 c2 c5
0 c1 0 0
*/
class Dice {
private:
  int yoko;
  int tate;
  int round;

private:
  int face[3][4];

public:
  int getFace(const int idx) const {
    if (idx == 0)
      return face[1][1];
    else if (idx == 1)
      return face[2][1];
    else if (idx == 2)
      return face[1][2];
    else if (idx == 3)
      return face[1][0];
    else if (idx == 4)
      return face[0][1];
    else if (idx == 5)
      return face[1][3];
    return -1;
  }
  Dice() {}
  Dice(vector<int> v) {
    init();
    face[1][1] = v[0];
    face[2][1] = v[1];
    face[1][2] = v[2];
    face[1][0] = v[3];
    face[0][1] = v[4];
    face[1][3] = v[5];
  }

private:
  // _CXÌú»
  void init() {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 4; j++)
        face[i][j] = -1;
    yoko = 0;
    tate = 0;
    round = 0;
  }

public:
  // _CXÌñ]
  void rotate() {
    if (round < 4) {
      int tmp = face[2][1];
      face[2][1] = face[1][2];
      face[1][2] = face[0][1];
      face[0][1] = face[1][0];
      face[1][0] = tmp;
      round++;
    }
    if (round >= 4) {
      round = 0;
      // àÇÁÄ«½çA¡ÌXbgðñ]³¹é
      if (yoko < 4) {
        int tmp = face[1][3];
        for (int i = 2; i >= 0; i--)
          face[1][i + 1] = face[1][i];
        face[1][0] = tmp;
        yoko++;
      }
      // àÇÁÄ«½çAcÌXbgðñ]³¹é
      if (yoko >= 4) {
        if (tate < 4) {
          int loop = 1;
          if (tate == 1)
            loop = 2;
          while (loop--) {
            tate++;
            int tmp = face[1][3];
            face[1][3] = face[0][1];
            for (int i = 0; i < 2; i++)
              face[i][1] = face[i + 1][1];
            face[2][1] = tmp;
          }
        }
        if (tate >= 4) {
          round = 0;
          tate = 0;
          yoko = 0;
        }
      }
    }
  };
};

vector<int> idxs;
int cnt;
Dice dices[8];
void dfs(int pos) {
  if (pos == 0) {
    for (int i = 0; i < 24; i++) {
      dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 1) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[0]].getFace(2);
      int ch2 = dices[idxs[pos]].getFace(3);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 2) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[1]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 3) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[2]].getFace(3);
      int ch2 = dices[idxs[pos]].getFace(2);
      int ch3 = dices[idxs[0]].getFace(1);
      int ch4 = dices[idxs[pos]].getFace(4);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 4) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[0]].getFace(5);
      int ch2 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 5) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[4]].getFace(2);
      int ch2 = dices[idxs[pos]].getFace(3);
      int ch3 = dices[idxs[1]].getFace(5);
      int ch4 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 6) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[5]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      int ch3 = dices[idxs[2]].getFace(5);
      int ch4 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 7) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[4]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      int ch3 = dices[idxs[6]].getFace(3);
      int ch4 = dices[idxs[pos]].getFace(2);
      int ch5 = dices[idxs[3]].getFace(5);
      int ch6 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32 && abs(ch5 - ch6) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else
    cnt++;
}

// bool search(){
//     cnt=0;
//     for(int i=0;i<8;i++)idxs[i]=i;
//     do{
//         for(int i=0;i<24;i++){
//             for(int j=0;j<24;j++){
//                 // Êª¤©`FbN
//                 int ch1=dices[idxs[0]].getFace(2);
//                 int ch2=dices[idxs[1]].getFace(3);
//                 if(abs(ch1-ch2)==32){
//                     for(int k=0;k<24;k++){
//                         // Êª¤©`FbN
//                         int ch1=dices[idxs[1]].getFace(1);
//                         int ch2=dices[idxs[2]].getFace(4);
//                         if(abs(ch1-ch2)==32){
//                             for(int l=0;l<24;l++){
//                                 // Êª¤©`FbN
//                                 int ch1=dices[idxs[2]].getFace(3);
//                                 int ch2=dices[idxs[3]].getFace(2);
//                                 int ch3=dices[idxs[0]].getFace(1);
//                                 int ch4=dices[idxs[3]].getFace(4);
//                                 if(abs(ch1-ch2)==32&&abs(ch3-ch4)==32){
//                                     for(int m=0;m<24;m++){
//                                         // Êª¤©`FbN
//                                         int ch1=dices[idxs[0]].getFace(5);
//                                         int ch2=dices[idxs[4]].getFace(0);
//                                         if(abs(ch1-ch2)==32){
//                                             for(int n=0;n<24;n++){
//                                                 //
//                                                 Êª¤©`FbN
//                                                 int
//                                                 ch1=dices[idxs[4]].getFace(2);
//                                                 int
//                                                 ch2=dices[idxs[5]].getFace(3);
//                                                 int
//                                                 ch3=dices[idxs[1]].getFace(5);
//                                                 int
//                                                 ch4=dices[idxs[5]].getFace(0);
//                                                 if(abs(ch1-ch2)==32&&abs(ch3-ch4)==32){
//                                                     for(int o=0;o<24;o++){
//                                                         //
//                                                         Êª¤©`FbN
//                                                         int
//                                                         ch1=dices[idxs[5]].getFace(1);
//                                                         int
//                                                         ch2=dices[idxs[6]].getFace(4);
//                                                         int
//                                                         ch3=dices[idxs[2]].getFace(5);
//                                                         int
//                                                         ch4=dices[idxs[6]].getFace(0);
//                                                         if(abs(ch1-ch2)==32&&abs(ch3-ch4)==32){
//                                                             for(int
//                                                             p=0;p<24;p++){
//                                                                 //
//                                                                 Êª¤©`FbN
//                                                                 int
//                                                                 ch1=dices[idxs[4]].getFace(1);
//                                                                 int
//                                                                 ch2=dices[idxs[7]].getFace(4);
//                                                                 int
//                                                                 ch3=dices[idxs[6]].getFace(3);
//                                                                 int
//                                                                 ch4=dices[idxs[7]].getFace(2);
//                                                                 int
//                                                                 ch5=dices[idxs[3]].getFace(5);
//                                                                 int
//                                                                 ch6=dices[idxs[7]].getFace(0);
//                                                                 if(abs(ch1-ch2)==32&&abs(ch3-ch4)==32&&abs(ch5-ch6)==32){
//                                                                     return
//                                                                     true;
//                                                                 }
//                                                                 dices[idxs[7]].rotate();
//                                                             }
//                                                         }
//                                                         dices[idxs[6]].rotate();
//                                                     }
//                                                 }
//                                                 dices[idxs[5]].rotate();
//                                             }
//                                         }
//                                         dices[idxs[4]].rotate();
//                                     }
//                                 }
//                                 dices[idxs[3]].rotate();
//                             }
//                         }
//                         dices[idxs[2]].rotate();
//                     }
//                 }
//                 dices[idxs[1]].rotate();
//             }
//             dices[idxs[0]].rotate();
//         }
//         //dfs(0);
//         //if(cnt>0)break;
//     }while(next_permutation(idxs,idxs+8));
//     return false;
// }

int main() {
  string s;
  while (getline(cin, s) && s != "0") {
    vector<int> d;
    for (int i = 0; i < s.size(); i++)
      d.push_back(s[i]);
    dices[0] = Dice(d);
    for (int i = 0; i < 7; i++) {
      d.clear();
      getline(cin, s);
      for (int j = 0; j < s.size(); j++)
        d.push_back(s[j]);
      dices[i + 1] = Dice(d);
    }
    cnt = 0;
    idxs.clear();
    for (int i = 0; i < 8; i++)
      idxs.push_back(i);
    // ·ÅÉTõ³ê½àÌðÂÁ±ñÅ¨­
    set<vector<int>> searched;
    do {
      if (searched.find(idxs) != searched.end()) {
        continue;
      }
      dfs(0);
      for (int i = 0; i < 4; i++) {
        searched.insert(idxs);
        for (int base = 0; base < 2; base++) {
          int a = base * 4;
          int tmp = idxs[3 + a];
          idxs[3 + a] = idxs[2 + a];
          idxs[2 + a] = idxs[1 + a];
          idxs[1 + a] = idxs[0 + a];
          idxs[a] = tmp;
        }
      }
      if (cnt > 0)
        break;
    } while (next_permutation(idxs.begin(), idxs.end()));
    // if(search())cnt++;
    if (cnt > 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

// 24ñÅ³ÉàÇé
/*
zñÆ_CXÌÊÌÖW
c0ÍRXgN^ÌøÅ^¦çêéxN^Ìvf
0 c4 0 0
c3 c0 c2 c5
0 c1 0 0
*/
class Dice {
private:
  int yoko;
  int tate;
  int round;

private:
  int face[3][4];

public:
  int getFace(const int idx) const {
    if (idx == 0)
      return face[1][1];
    else if (idx == 1)
      return face[2][1];
    else if (idx == 2)
      return face[1][2];
    else if (idx == 3)
      return face[1][0];
    else if (idx == 4)
      return face[0][1];
    else if (idx == 5)
      return face[1][3];
    return -1;
  }
  Dice() {}
  Dice(vector<int> v) {
    init();
    face[1][1] = v[0];
    face[2][1] = v[1];
    face[1][2] = v[2];
    face[1][0] = v[3];
    face[0][1] = v[4];
    face[1][3] = v[5];
  }

private:
  // _CXÌú»
  void init() {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 4; j++)
        face[i][j] = -1;
    yoko = 0;
    tate = 0;
    round = 0;
  }

public:
  // _CXÌñ]
  void rotate() {
    if (round < 4) {
      int tmp = face[2][1];
      face[2][1] = face[1][2];
      face[1][2] = face[0][1];
      face[0][1] = face[1][0];
      face[1][0] = tmp;
      round++;
    }
    if (round >= 4) {
      round = 0;
      // àÇÁÄ«½çA¡ÌXbgðñ]³¹é
      if (yoko < 4) {
        int tmp = face[1][3];
        for (int i = 2; i >= 0; i--)
          face[1][i + 1] = face[1][i];
        face[1][0] = tmp;
        yoko++;
      }
      // àÇÁÄ«½çAcÌXbgðñ]³¹é
      if (yoko >= 4) {
        if (tate < 4) {
          int loop = 1;
          if (tate == 1)
            loop = 2;
          while (loop--) {
            tate++;
            int tmp = face[1][3];
            face[1][3] = face[0][1];
            for (int i = 0; i < 2; i++)
              face[i][1] = face[i + 1][1];
            face[2][1] = tmp;
          }
        }
        if (tate >= 4) {
          round = 0;
          tate = 0;
          yoko = 0;
        }
      }
    }
  };
};

int idxs[101];
int cnt;
Dice dices[8];
void dfs(int pos) {
  if (pos == 0) {
    for (int i = 0; i < 24; i++) {
      dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 1) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[0]].getFace(2);
      int ch2 = dices[idxs[pos]].getFace(3);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 2) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[1]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 3) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[2]].getFace(3);
      int ch2 = dices[idxs[pos]].getFace(2);
      int ch3 = dices[idxs[0]].getFace(1);
      int ch4 = dices[idxs[pos]].getFace(4);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 4) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[0]].getFace(5);
      int ch2 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 5) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[4]].getFace(2);
      int ch2 = dices[idxs[pos]].getFace(3);
      int ch3 = dices[idxs[1]].getFace(5);
      int ch4 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 6) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[5]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      int ch3 = dices[idxs[2]].getFace(5);
      int ch4 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else if (pos == 7) {
    for (int i = 0; i < 24; i++) {
      // Êª¤©`FbN
      int ch1 = dices[idxs[4]].getFace(1);
      int ch2 = dices[idxs[pos]].getFace(4);
      int ch3 = dices[idxs[6]].getFace(3);
      int ch4 = dices[idxs[pos]].getFace(2);
      int ch5 = dices[idxs[3]].getFace(5);
      int ch6 = dices[idxs[pos]].getFace(0);
      if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32 && abs(ch5 - ch6) == 32)
        dfs(pos + 1);
      if (cnt > 0)
        return;
      dices[idxs[pos]].rotate();
    }
  } else
    cnt++;
}

bool search() {
  cnt = 0;
  for (int i = 0; i < 8; i++)
    idxs[i] = i;
  do {
    for (int i = 0; i < 24; i++) {
      for (int j = 0; j < 24; j++) {
        // Êª¤©`FbN
        int ch1 = dices[idxs[0]].getFace(2);
        int ch2 = dices[idxs[1]].getFace(3);
        if (abs(ch1 - ch2) == 32) {
          for (int k = 0; k < 24; k++) {
            // Êª¤©`FbN
            int ch1 = dices[idxs[1]].getFace(1);
            int ch2 = dices[idxs[2]].getFace(4);
            if (abs(ch1 - ch2) == 32) {
              for (int l = 0; l < 24; l++) {
                // Êª¤©`FbN
                int ch1 = dices[idxs[2]].getFace(3);
                int ch2 = dices[idxs[3]].getFace(2);
                int ch3 = dices[idxs[0]].getFace(1);
                int ch4 = dices[idxs[3]].getFace(4);
                if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32) {
                  for (int m = 0; m < 24; m++) {
                    // Êª¤©`FbN
                    int ch1 = dices[idxs[0]].getFace(5);
                    int ch2 = dices[idxs[4]].getFace(0);
                    if (abs(ch1 - ch2) == 32) {
                      for (int n = 0; n < 24; n++) {
                        // Êª¤©`FbN
                        int ch1 = dices[idxs[4]].getFace(2);
                        int ch2 = dices[idxs[5]].getFace(3);
                        int ch3 = dices[idxs[1]].getFace(5);
                        int ch4 = dices[idxs[5]].getFace(0);
                        if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32) {
                          for (int o = 0; o < 24; o++) {
                            // Êª¤©`FbN
                            int ch1 = dices[idxs[5]].getFace(1);
                            int ch2 = dices[idxs[6]].getFace(4);
                            int ch3 = dices[idxs[2]].getFace(5);
                            int ch4 = dices[idxs[6]].getFace(0);
                            if (abs(ch1 - ch2) == 32 && abs(ch3 - ch4) == 32) {
                              for (int p = 0; p < 24; p++) {
                                // Êª¤©`FbN
                                int ch1 = dices[idxs[4]].getFace(1);
                                int ch2 = dices[idxs[7]].getFace(4);
                                int ch3 = dices[idxs[6]].getFace(3);
                                int ch4 = dices[idxs[7]].getFace(2);
                                int ch5 = dices[idxs[3]].getFace(5);
                                int ch6 = dices[idxs[7]].getFace(0);
                                if (abs(ch1 - ch2) == 32 &&
                                    abs(ch3 - ch4) == 32 &&
                                    abs(ch5 - ch6) == 32) {
                                  return true;
                                }
                                dices[idxs[7]].rotate();
                              }
                            }
                            dices[idxs[6]].rotate();
                          }
                        }
                        dices[idxs[5]].rotate();
                      }
                    }
                    dices[idxs[4]].rotate();
                  }
                }
                dices[idxs[3]].rotate();
              }
            }
            dices[idxs[2]].rotate();
          }
        }
        dices[idxs[1]].rotate();
      }
      dices[idxs[0]].rotate();
    }
    // dfs(0);
    // if(cnt>0)break;
  } while (next_permutation(idxs, idxs + 8));
  return false;
}

int main() {

  string s;
  while (getline(cin, s) && s != "0") {
    vector<int> d;
    for (int i = 0; i < s.size(); i++)
      d.push_back(s[i]);
    dices[0] = Dice(d);
    for (int i = 0; i < 7; i++) {
      d.clear();
      getline(cin, s);
      for (int j = 0; j < s.size(); j++)
        d.push_back(s[j]);
      dices[i + 1] = Dice(d);
    }
    // cnt=0;
    // for(int i=0;i<8;i++)idxs[i]=i;
    // do{
    //     //dfs(0);
    //     //if(cnt>0)break;
    // }while(next_permutation(idxs,idxs+8));
    if (search())
      cnt++;
    if (cnt > 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}