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

#include <iostream>
using namespace std;

class Dice {
  int _num[6];

public:
  void set(int num[6]) {
    for (int i = 0; i < 6; i++)
      _num[i] = num[i];
  }
  int Get(int n) { return _num[n]; }
  void N() {
    swap(_num[0], _num[4]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[1]);
  }
  void S() {
    swap(_num[0], _num[1]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[4]);
  }
  void W() {
    swap(_num[0], _num[3]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[2]);
  }
  void E() {
    swap(_num[0], _num[2]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[3]);
  }
  void SetTop(int n) {
    if (_num[2] == n)
      W();
    else if (_num[3] == n)
      E();
    else {
      for (int i = 0; i < 4; i++) {
        if (_num[0] == n)
          break;
        S();
      }
    }
  }
  void SetFront(int n) {
    for (int i = 0; i < 4; i++) {
      if (_num[1] == n)
        break;
      S();
      W();
      N();
    }
  }

  bool Same(Dice other) {
    other.SetTop(this->Get(0));
    other.SetFront(this->Get(1));

    bool same = true;
    for (int i = 0; i < 6; i++) {
      if (other.Get(i) != this->Get(i)) {
        same = false;
        break;
      }
    }
    return same;
  }
};

int main() {
  int n;
  cin >> n;

  Dice dice[n];
  for (int k = 0; k < n; k++) {
    int num[6];
    for (int l = 0; l < 6; l++) {
      cin >> num[l];
    }
    dice[k].set(num);
  }

  for (int p = 0; p < n; p++) {
    for (int q = p + 1; q < n; q++) {
      if (dice[p].Same(dice[q])) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Dice {
  int _num[6];

public:
  void set(int num[6]) {
    for (int i = 0; i < 6; i++)
      _num[i] = num[i];
  }
  void printTop() { cout << _num[0] << endl; }
  void printRight() { cout << _num[2] << endl; }
  void N() {
    swap(_num[0], _num[4]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[1]);
  }
  void S() {
    swap(_num[0], _num[1]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[4]);
  }
  void W() {
    swap(_num[0], _num[3]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[2]);
  }
  void E() {
    swap(_num[0], _num[2]);
    swap(_num[0], _num[5]);
    swap(_num[0], _num[3]);
  }
  void SetTop(int n) {
    if (_num[2] == n)
      W();
    else if (_num[3] == n)
      E();
    else {
      while (1) {
        if (_num[0] == n)
          break;
        S();
      }
    }
  }
  void SetFront(int n) {
    while (1) {
      if (_num[1] == n)
        break;
      S();
      W();
      N();
    }
  }
  int Get(int n) { return _num[n]; }
  bool SameAs(Dice one) {
    one.SetTop(this->Get(0));
    one.SetFront(this->Get(1));

    bool same = true;
    for (int i = 0; i < 6; i++) {
      if (one.Get(i) != this->Get(i)) {
        same = false;
        break;
      }
    }
    return same;
  }
};

int main() {
  int n;
  cin >> n;

  Dice dice[n];
  for (int i = 0; i < n; i++) {
    int num[6];
    for (int j = 0; j < 6; j++) {
      cin >> num[j];
    }
    dice[i].set(num);
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (dice[i].SameAs(dice[j])) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
  return 0;
}