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

#include <iostream>
using namespace std;

class Dice {
  int top, n, s, e, w;

public:
  Dice();
  void roll(char dir);
  void turn(char dir);
  int getNum(char dir);
};

Dice::Dice() {
  top = 1;
  n = 5;
  s = 2;
  e = 3;
  w = 4;
}

void Dice::roll(char dir) {
  switch (dir) {
  case 'N':
    n = top;
    top = s;
    s = 7 - n;
    break;
  case 'S':
    s = top;
    top = n;
    n = 7 - s;
    break;
  case 'E':
    e = top;
    top = w;
    w = 7 - e;
    break;
  case 'W':
    w = top;
    top = e;
    e = 7 - w;
    break;
  }
}

void Dice::turn(char dir) {
  switch (dir) {
  case 'R':
    n = w;
    w = s;
    s = e;
    e = 7 - w;
    break;
  case 'L':
    n = e;
    e = s;
    s = w;
    w = 7 - e;
    break;
  }
}

int Dice::getNum(char dir) {
  switch (dir) {
  case 'T':
    return top;
  case 'N':
    return n;
  case 'E':
    return e;
  case 'W':
    return w;
  case 'S':
    return s;
  case 'B':
    return 7 - top;
  default:
    return -1;
  }
}

int main(void) {
  int **d, n, c;
  Dice *dice;
  bool answer = true;

  cin >> n;

  d = new int *[n];
  dice = new Dice[n];

  for (int i = 0; i < n; i++) {
    d[i] = new int[6];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 6; j++) {
      cin >> d[i][j];
    }
  }

  for (int i = 0; i < n - 1; i++) {
    c = 0;
    while (d[0][dice[0].getNum('T') - 1] !=
           d[i + 1][dice[i + 1].getNum('T') - 1]) {
      if (c < 4)
        dice[i + 1].roll('S');
      else
        dice[i + 1].roll('E');
      c++;
      if (c == 8)
        break;
    }
    c = 0;
    while (d[0][dice[0].getNum('S') - 1] !=
           d[i + 1][dice[i + 1].getNum('S') - 1]) {
      dice[i + 1].turn('R');
      c++;
      if (c == 5)
        break;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (d[i][dice[i].getNum('T') - 1] == d[j][dice[j].getNum('T') - 1] &&
          d[i][dice[i].getNum('E') - 1] == d[j][dice[j].getNum('E') - 1] &&
          d[i][dice[i].getNum('N') - 1] == d[j][dice[j].getNum('N') - 1] &&
          d[i][dice[i].getNum('W') - 1] == d[j][dice[j].getNum('W') - 1] &&
          d[i][dice[i].getNum('S') - 1] == d[j][dice[j].getNum('S') - 1] &&
          d[i][dice[i].getNum('B') - 1] == d[j][dice[j].getNum('B') - 1]) {

        i = j = n;
        answer = false;
      }
    }
  }

  if (answer) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Dice {
  int top, n, s, e, w;

public:
  Dice();
  void roll(char dir);
  void turn(char dir);
  int getNum(char dir);
};

Dice::Dice() {
  top = 1;
  n = 5;
  s = 2;
  e = 3;
  w = 4;
}

void Dice::roll(char dir) {
  switch (dir) {
  case 'N':
    n = top;
    top = s;
    s = 7 - n;
    break;
  case 'S':
    s = top;
    top = n;
    n = 7 - s;
    break;
  case 'E':
    e = top;
    top = w;
    w = 7 - e;
    break;
  case 'W':
    w = top;
    top = e;
    e = 7 - w;
    break;
  }
}

void Dice::turn(char dir) {
  switch (dir) {
  case 'R':
    n = w;
    w = s;
    s = e;
    e = 7 - w;
    break;
  case 'L':
    n = e;
    e = s;
    s = w;
    w = 7 - e;
    break;
  }
}

int Dice::getNum(char dir) {
  switch (dir) {
  case 'T':
    return top;
  case 'N':
    return n;
  case 'E':
    return e;
  case 'W':
    return w;
  case 'S':
    return s;
  case 'B':
    return 7 - top;
  default:
    return -1;
  }
}

int main(void) {
  int **d, n;
  Dice *dice;
  bool answer = true;

  cin >> n;

  d = new int *[n];
  dice = new Dice[n];

  for (int i = 0; i < n; i++) {
    d[i] = new int[6];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 6; j++) {
      cin >> d[i][j];
    }
  }

  for (int i = 0, c = 0; i < n - 1; i++) {
    while (d[i][dice[i].getNum('T') - 1] !=
           d[i + 1][dice[i + 1].getNum('T') - 1]) {
      if (c < 4)
        dice[i + 1].roll('S');
      else
        dice[i + 1].roll('E');
      c++;
    }
    while (d[i][dice[i].getNum('S') - 1] !=
           d[i + 1][dice[i + 1].getNum('S') - 1]) {
      dice[i + 1].turn('R');
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (d[i][dice[i].getNum('E') - 1] == d[j][dice[j].getNum('E') - 1])
        answer = false;
    }
  }

  if (answer) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }

  return 0;
}