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

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

class Dice {
private:
  int face[6];

public:
  int getTop();
  int getFace(int);
  void setFace(int label, int value);
  void turnOver(char dir);
  void debugOut();
};

int Dice::getTop() { return face[0]; }

int Dice::getFace(int i) { return face[i]; }

void Dice::setFace(int label, int value) {
  if (label > 5)
    return;
  face[label] = value;
}

void Dice::turnOver(char c) {
  int tmp;
  if (c == 'N') {
    tmp = face[0];
    face[0] = face[1];
    face[1] = face[5];
    face[5] = face[4];
    face[4] = tmp; // face[0]
  }

  if (c == 'S') {
    tmp = face[0];
    face[0] = face[4];
    face[4] = face[5];
    face[5] = face[1];
    face[1] = tmp; // face[0]
  }

  if (c == 'E') {
    tmp = face[0];
    face[0] = face[3];
    face[3] = face[5];
    face[5] = face[2];
    face[2] = tmp; // face[4]
  }

  if (c == 'W') {
    tmp = face[0];
    face[0] = face[2];
    face[2] = face[5];
    face[5] = face[3];
    face[3] = tmp; // face[4]
  }
}

void Dice::debugOut() {
  for (int i = 0; i < 6; i++)
    printf("%d,", face[i]);
  putchar('\\n');
}

int main() {
  Dice dice;
  Dice dice2;
  int l, count_n = 0;

  for (int i = 0; i < 6; i++) {
    scanf("%d ", &l);
    dice.setFace(i, l);
  }
  for (int i = 0; i < 6; i++) {
    scanf("%d ", &l);
    dice2.setFace(i, l);
  }
  for (;;) {
    if (dice.getFace(0) == dice2.getFace(0)) {
      if (dice.getFace(1) == dice2.getFace(1)) {
        if (dice.getFace(2) == dice2.getFace(2)) {
          if (dice.getFace(3) == dice2.getFace(3)) {
            if (dice.getFace(4) == dice2.getFace(4)) {
              if (dice.getFace(5) == dice2.getFace(5)) {
                puts("Yes");
                return 0;
              } else {
                puts("No");
                return 0;
              }
            }
          } else {
            dice2.turnOver('E');
            dice2.turnOver('S');
            dice2.turnOver('S');
          }
        } else {
          puts("No");
          return 0;
        }
      } else {
        dice2.turnOver('W');
        dice2.turnOver('S');
        dice2.turnOver('E');
      }
    } else {
      if (count_n < 3) {
        dice2.turnOver('N');
        count_n++;
      } else {
        count_n = 0;
        dice2.turnOver('E');
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

class Dice {
private:
  int face[6];

public:
  int getTop();
  int getFace(int);
  void setFace(int label, int value);
  void turnOver(char dir);
  void debugOut();
};

int Dice::getTop() { return face[0]; }

int Dice::getFace(int i) { return face[i]; }

void Dice::setFace(int label, int value) {
  if (label > 5)
    return;
  face[label] = value;
}

void Dice::turnOver(char c) {
  int tmp;
  if (c == 'N') {
    tmp = face[0];
    face[0] = face[1];
    face[1] = face[5];
    face[5] = face[4];
    face[4] = tmp; // face[0]
  }

  if (c == 'S') {
    tmp = face[0];
    face[0] = face[4];
    face[4] = face[5];
    face[5] = face[1];
    face[1] = tmp; // face[0]
  }

  if (c == 'E') {
    tmp = face[0];
    face[0] = face[3];
    face[3] = face[5];
    face[5] = face[2];
    face[2] = tmp; // face[4]
  }

  if (c == 'W') {
    tmp = face[0];
    face[0] = face[2];
    face[2] = face[5];
    face[5] = face[3];
    face[3] = tmp; // face[4]
  }
}

void Dice::debugOut() {
  for (int i = 0; i < 6; i++)
    printf("%d,", face[i]);
  putchar('\\n');
}

int main() {
  Dice dice;
  Dice dice2;
  int l, count_n = 0;

  for (int i = 0; i < 6; i++) {
    scanf("%d ", &l);
    dice.setFace(i, l);
  }
  for (int i = 0; i < 6; i++) {
    scanf("%d ", &l);
    dice2.setFace(i, l);
  }
  for (;;) {
    if (dice.getFace(0) == dice2.getFace(0)) {
      if (dice.getFace(1) == dice2.getFace(1)) {
        if (dice.getFace(2) == dice2.getFace(2)) {
          if (dice.getFace(3) == dice2.getFace(3)) {
            if (dice.getFace(4) == dice2.getFace(4)) {
              if (dice.getFace(5) == dice2.getFace(5)) {
                puts("Yes");
                return 0;
              } else {
                puts("No");
                return 0;
              }
            }
          } else {
            dice2.turnOver('E');
            dice2.turnOver('S');
            dice2.turnOver('S');
          }
        }
      } else {
        dice2.turnOver('W');
        dice2.turnOver('S');
        dice2.turnOver('E');
      }
    } else {
      if (count_n < 3) {
        dice2.turnOver('N');
        count_n++;
      } else {
        count_n = 0;
        dice2.turnOver('E');
      }
    }
  }
}