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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

/*  ________
   / D /   /|
  /---/---/C|
 /__ /__ /|/|
| A | B | /G|
|___|___|/|/
| E | F | /
|___|___|/
*/

int rotx(string &dice) {
  char t = dice[0];
  dice[0] = dice[1];
  dice[1] = dice[5];
  dice[5] = dice[4];
  dice[4] = t;
}

int roty(string &dice) {
  char t = dice[0];
  dice[0] = dice[3];
  dice[3] = dice[5];
  dice[5] = dice[2];
  dice[2] = t;
}

int rotz(string &dice) {
  char t = dice[1];
  dice[1] = dice[3];
  dice[3] = dice[4];
  dice[4] = dice[2];
  dice[2] = t;
}

void next(string &dice, int i) {
  if (i % 4 == 0) {
    if (i % 8 == 0)
      rotx(dice);
    else
      roty(dice);
  }
  rotz(dice);
}
/*
int up[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,1,3,2,4,0}
};
*/

#define PRUNING

int main() {
  string dice[8];
  while (1) {
    for (int i = 0; i < 8; i++)
      cin >> dice[i];
    if (dice[0] == "0")
      break;

    char co[128];
    for (int c = 'A'; c <= 'Z'; c++)
      co[c] = tolower(c);
    for (int c = 'a'; c <= 'z'; c++)
      co[c] = toupper(c);

    bool exist[8][128] = {};
    for (int i = 0; i < 8; i++)
      for (int j = 0; j < 6; j++)
        exist[i][dice[i][j]] = true;

    bool ok = false;
    int i[] = {0, 1, 2, 3, 4, 5, 6, 7};
    int a, b, c, d, e, f, g, h;
    string diceA, diceB, diceC, diceD, diceE, diceF, diceG, diceH;
    do { // fix the order of 8 dices
      a = i[0];
      b = i[1];
      c = i[2];
      d = i[3];
      e = i[4];
      f = i[5];
      g = i[6];
      h = i[7];
      diceA = dice[a];
      diceB = dice[b];
      diceC = dice[c];
      diceD = dice[d];
      diceE = dice[e];
      diceF = dice[f];
      diceG = dice[g];
      diceH = dice[h];

      for (int ia = 0; ia < 24;
           ia++, next(diceA, ia)) { // fix diceA's direction
#ifdef PRUNING
        if (!exist[b][co[diceA[2]]])
          continue;
        if (!exist[e][co[diceA[5]]])
          continue;
#endif
        for (int ib = 0; ib < 24;
             ib++, next(diceB, ib)) { // fix diceB's direction
          if (co[diceA[2]] != diceB[3])
            continue;
#ifdef PRUNING
          if (!exist[c][co[diceB[4]]])
            continue;
          if (!exist[f][co[diceB[5]]])
            continue;
#endif
          for (int ic = 0; ic < 24; ic++, next(diceC, ic)) { // fix diceC's ...
            if (co[diceB[4]] != diceC[1])
              continue;
#ifdef PRUNING
            if (!exist[d][co[diceA[4]]] || !exist[d][co[diceC[3]]])
              continue;
            if (!exist[g][co[diceC[5]]])
              continue;
#endif
            for (int id = 0; id < 24; id++, next(diceD, id)) {
              if (co[diceA[4]] != diceD[1] || co[diceC[3]] != diceD[2])
                continue;
#ifdef PRUNING
              if (!exist[h][co[diceD[5]]])
                continue;
#endif
              for (int ie = 0; ie < 24; ie++, next(diceE, ie)) {
                if (co[diceA[5]] != diceE[0])
                  continue;
                for (int iF = 0; iF < 24; iF++, next(diceF, iF)) {
                  if (co[diceB[5]] != diceF[0] || co[diceE[2]] != diceF[3])
                    continue;
                  for (int ig = 0; ig < 24; ig++, next(diceG, ig)) {
                    if (co[diceC[5]] != diceG[0] || co[diceF[4]] != diceG[1])
                      continue;
                    for (int ih = 0; ih < 24; ih++, next(diceH, ih)) {
                      if (co[diceD[5]] != diceH[0] ||
                          co[diceE[4]] != diceH[1] || co[diceG[3]] != diceH[2])
                        continue;
                      ok = true;
                      goto FOUND;
                    }
                  }
                }
              }
            }
          }
        }
      }
    } while (next_permutation(i + 1, i + 8));

  FOUND:
    cout << (ok ? "YES" : "NO") << endl;
  }

  return 0;
}

/*	TLE
int rotx(string &dice){
        char t=dice[0];
        dice[0]=dice[1];
        dice[1]=dice[5];
        dice[5]=dice[4];
        dice[4]=t;
}

int roty(string &dice){
        char t=dice[0];
        dice[0]=dice[3];
        dice[3]=dice[5];
        dice[5]=dice[2];
        dice[2]=t;
}

int rotz(string &dice){
        char t=dice[1];
        dice[1]=dice[3];
        dice[3]=dice[4];
        dice[4]=dice[2];
        dice[2]=t;
}

void next(string &dice,int i){
        if(i%4==0){
                if(i%8==0)	rotx(dice);
                else		roty(dice);
        }
        rotz(dice);
}

bool match(char a,char b){
        return (a!=b)&&(toupper(a)==toupper(b));
}

int main(){
        string dice[8];
        while(1){
                for(int i=0;i<8;i++)	cin>>dice[i];
                if(dice[0]=="0")	break;

                bool ok=false;
                int i[]={0,1,2,3,4,5,6,7};
                string diceA,diceB,diceC,diceD,diceE,diceF,diceG,diceH;
                do{	// fix the order of 8 dices
                        diceA=dice[i[0]];
                        diceB=dice[i[1]];
                        diceC=dice[i[2]];
                        diceD=dice[i[3]];
                        diceE=dice[i[4]];
                        diceF=dice[i[5]];
                        diceG=dice[i[6]];
                        diceH=dice[i[7]];
                        for(int ia=0;ia<24;ia++,next(diceA,ia)){	// fix
diceA's direction for(int ib=0;ib<24;ib++,next(diceB,ib)){	// fix diceB's
direction if(!match(diceA[2],diceB[3]))	continue; for(int
ic=0;ic<24;ic++,next(diceC,ic)){	// fix diceC's ...
                                                if(!match(diceB[4],diceC[1]))
continue; for(int id=0;id<24;id++,next(diceD,id)){ if(!match(diceA[4],diceD[1])
|| !match(diceC[3],diceD[2]))	continue; for(int
ie=0;ie<24;ie++,next(diceE,ie)){ if(!match(diceA[5],diceE[0]))	continue;
                                                                for(int
iF=0;iF<24;iF++,next(diceF,iF)){ if(!match(diceB[5],diceF[0]) ||
!match(diceE[2],diceF[3]))	continue; for(int
ig=0;ig<24;ig++,next(diceG,ig)){ if(!match(diceC[5],diceG[0]) ||
!match(diceF[4],diceG[1]))	continue; for(int
ih=0;ih<24;ih++,next(diceH,ih)){ if(!match(diceD[5],diceH[0]) ||
!match(diceE[4],diceH[1]) || !match(diceG[3],diceH[2]))	continue; ok=true; goto
FOUND;
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }while(next_permutation(i,i+8));

                FOUND:
                cout<<(ok?"YES":"NO")<<endl;
        }

        return 0;
}
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

/*  ________
   / D /   /|
  /---/---/C|
 /__ /__ /|/|
| A | B | /G|
|___|___|/|/
| E | F | /
|___|___|/
*/

int rotx(string &dice) {
  char t = dice[0];
  dice[0] = dice[1];
  dice[1] = dice[5];
  dice[5] = dice[4];
  dice[4] = t;
}

int roty(string &dice) {
  char t = dice[0];
  dice[0] = dice[3];
  dice[3] = dice[5];
  dice[5] = dice[2];
  dice[2] = t;
}

int rotz(string &dice) {
  char t = dice[1];
  dice[1] = dice[3];
  dice[3] = dice[4];
  dice[4] = dice[2];
  dice[2] = t;
}

void next(string &dice, int i) {
  if (i % 4 == 0) {
    if (i % 8 == 0)
      rotx(dice);
    else
      roty(dice);
  }
  rotz(dice);
}
/*
int up[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,1,3,2,4,0}
};
*/

int main() {
  string dice[8];
  while (1) {
    for (int i = 0; i < 8; i++)
      cin >> dice[i];
    if (dice[0] == "0")
      break;

    char co[128];
    for (int c = 'A'; c <= 'Z'; c++)
      co[c] = tolower(c);
    for (int c = 'a'; c <= 'z'; c++)
      co[c] = toupper(c);

    bool exist[8][128] = {};
    for (int i = 0; i < 8; i++)
      for (int j = 0; j < 6; j++)
        exist[i][dice[i][j]] = true;

    bool ok = false;
    int i[] = {0, 1, 2, 3, 4, 5, 6, 7};
    int a, b, c, d, e, f, g, h;
    string diceA, diceB, diceC, diceD, diceE, diceF, diceG, diceH;
    do { // fix the order of 8 dices
      a = i[0];
      b = i[1];
      c = i[2];
      d = i[3];
      e = i[4];
      f = i[5];
      g = i[6];
      h = i[7];
      diceA = dice[a];
      diceB = dice[b];
      diceC = dice[c];
      diceD = dice[d];
      diceE = dice[e];
      diceF = dice[f];
      diceG = dice[g];
      diceH = dice[h];

      for (int ia = 0; ia < 24;
           ia++, next(diceA, ia)) { // fix diceA's direction
        if (!exist[b][co[diceA[2]]])
          continue;
        for (int ib = 0; ib < 24;
             ib++, next(diceB, ib)) { // fix diceB's direction
          if (co[diceA[2]] != diceB[3])
            continue;
          for (int ic = 0; ic < 24; ic++, next(diceC, ic)) { // fix diceC's ...
            if (co[diceB[4]] != diceC[1])
              continue;
            for (int id = 0; id < 24; id++, next(diceD, id)) {
              if (co[diceA[4]] != diceD[1] || co[diceC[3]] != diceD[2])
                continue;
              for (int ie = 0; ie < 24; ie++, next(diceE, ie)) {
                if (co[diceA[5]] != diceE[0])
                  continue;
                for (int iF = 0; iF < 24; iF++, next(diceF, iF)) {
                  if (co[diceB[5]] != diceF[0] || co[diceE[2]] != diceF[3])
                    continue;
                  for (int ig = 0; ig < 24; ig++, next(diceG, ig)) {
                    if (co[diceC[5]] != diceG[0] || co[diceF[4]] != diceG[1])
                      continue;
                    for (int ih = 0; ih < 24; ih++, next(diceH, ih)) {
                      if (co[diceD[5]] != diceH[0] ||
                          co[diceE[4]] != diceH[1] || co[diceG[3]] != diceH[2])
                        continue;
                      ok = true;
                      goto FOUND;
                    }
                  }
                }
              }
            }
          }
        }
      }
    } while (next_permutation(i, i + 8));

  FOUND:
    cout << (ok ? "YES" : "NO") << endl;
  }

  return 0;
}