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

#include <iostream>
#include <string>
using namespace std;

enum eRoll { W, S, N, E };

class Dice {
private:
  int _dice[6];

public:
  void roll(eRoll er) {
    int _diceinfo[6];
    for (int i = 0; i < 6; ++i) {
      _diceinfo[i] = _dice[i];
    }
    switch (er) {
    case W:
      _dice[0] = _diceinfo[2];
      _dice[1] = _diceinfo[1];
      _dice[2] = _diceinfo[5];
      _dice[3] = _diceinfo[0];
      _dice[4] = _diceinfo[4];
      _dice[5] = _diceinfo[3];
      break;
    case S:
      _dice[0] = _diceinfo[4];
      _dice[1] = _diceinfo[0];
      _dice[2] = _diceinfo[2];
      _dice[3] = _diceinfo[3];
      _dice[4] = _diceinfo[5];
      _dice[5] = _diceinfo[1];
      break;
    case N:
      _dice[0] = _diceinfo[1];
      _dice[1] = _diceinfo[5];
      _dice[2] = _diceinfo[2];
      _dice[3] = _diceinfo[3];
      _dice[4] = _diceinfo[0];
      _dice[5] = _diceinfo[4];
      break;
    case E:
      _dice[0] = _diceinfo[3];
      _dice[1] = _diceinfo[1];
      _dice[2] = _diceinfo[0];
      _dice[3] = _diceinfo[5];
      _dice[4] = _diceinfo[4];
      _dice[5] = _diceinfo[2];
      break;
    }
  }
  int getTopNum() { return _dice[0]; }
  int getRSideNum() { return _dice[2]; }
  int getForwardNum() { return _dice[1]; }
  int getNum(int index) { return _dice[index]; }

  void setNum() {
    for (int i = 0; i < 6; ++i)
      cin >> _dice[i];
  }
};

bool numCheck(Dice dice1, Dice dice2) {
  bool equal = false;
  int work[6] = {-1, -1, -1, -1, -1, -1};
  int counter = 0;
  for (int i = 0; i < 6; ++i) {
    for (int k = 0; k < 6; ++k) {
      for (int j = 0; j < 6; ++j) {
        if (k == work[j])
          continue;
      }
      if (dice1.getNum(i) == dice2.getNum(k)) {
        equal = true;

        work[counter] = k;
        ++counter;
        break;
      }
    }
    if (!equal)
      return false;
  }

  return true;
}

bool IsEqual(Dice dice1, Dice dice2) {
  bool work = true;
  for (int i = 0; i < 6; ++i) {
    if (dice1.getNum(i) != dice2.getNum(i)) {
      work = false;
      break;
    }
  }
  if (work)
    return true;
  dice1.roll(W);
  dice1.roll(W);
  for (int i = 0; i < 6; ++i) {
    if (dice1.getNum(i) != dice2.getNum(i)) {
      dice1.roll(W);
      dice1.roll(W);
      return false;
    }
  }
  return true;
}

int main() {
  int n;

  cin >> n;

  Dice *dice = new Dice[n];

  for (int i = 0; i < n; ++i) {
    dice[i].setNum();
  }

  for (int ix = 0; ix < n - 1; ++ix) {
    for (int ixx = ix + 1; ixx < n; ++ixx) {
      if (!numCheck(dice[ix], dice[ixx])) {
        continue;
      }

      for (int i = 0; i < 3; ++i) {
        for (int k = 0; k < 4; ++k) {
          if (IsEqual(dice[ix], dice[ixx])) {
            cout << "No" << endl;
            // while (true) {}
            return 0;
          }
          dice[ixx].roll(N);
        }
        if (i == 0)
          dice[ixx].roll(W);
        if (i == 1) {
          dice[ixx].roll(N);
          dice[ixx].roll(W);
        }
      }
    }
  }
  delete[] dice;

  cout << "Yes" << endl;
  // while(true){}
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

enum eRoll { W, S, N, E };

class Dice {
private:
  int _dice[6];

public:
  void roll(eRoll er) {
    int _diceinfo[6];
    for (int i = 0; i < 6; ++i) {
      _diceinfo[i] = _dice[i];
    }
    switch (er) {
    case W:
      _dice[0] = _diceinfo[2];
      _dice[1] = _diceinfo[1];
      _dice[2] = _diceinfo[5];
      _dice[3] = _diceinfo[0];
      _dice[4] = _diceinfo[4];
      _dice[5] = _diceinfo[3];
      break;
    case S:
      _dice[0] = _diceinfo[4];
      _dice[1] = _diceinfo[0];
      _dice[2] = _diceinfo[2];
      _dice[3] = _diceinfo[3];
      _dice[4] = _diceinfo[5];
      _dice[5] = _diceinfo[1];
      break;
    case N:
      _dice[0] = _diceinfo[1];
      _dice[1] = _diceinfo[5];
      _dice[2] = _diceinfo[2];
      _dice[3] = _diceinfo[3];
      _dice[4] = _diceinfo[0];
      _dice[5] = _diceinfo[4];
      break;
    case E:
      _dice[0] = _diceinfo[3];
      _dice[1] = _diceinfo[1];
      _dice[2] = _diceinfo[0];
      _dice[3] = _diceinfo[5];
      _dice[4] = _diceinfo[4];
      _dice[5] = _diceinfo[2];
      break;
    }
  }
  int getTopNum() { return _dice[0]; }
  int getRSideNum() { return _dice[2]; }
  int getForwardNum() { return _dice[1]; }
  int getNum(int index) { return _dice[index]; }

  void setNum() {
    for (int i = 0; i < 6; ++i)
      cin >> _dice[i];
  }
};

bool numCheck(Dice dice1, Dice dice2) {
  bool equal = false;
  int work[6] = {-1, -1, -1, -1, -1, -1};
  int counter = 0;
  for (int i = 0; i < 6; ++i) {
    for (int k = 0; k < 6; ++k) {
      for (int j = 0; j < 6; ++j) {
        if (k == work[j])
          continue;
      }
      if (dice1.getNum(i) == dice2.getNum(k)) {
        equal = true;

        work[counter] = k;
        ++counter;
        break;
      }
    }
    if (!equal)
      return false;
  }

  return true;
}

bool IsEqual(Dice dice1, Dice dice2) {
  bool work = true;
  for (int i = 0; i < 6; ++i) {
    if (dice1.getNum(i) != dice2.getNum(i)) {
      work = false;
      break;
    }
  }
  if (work)
    return true;
  dice1.roll(W);
  dice1.roll(W);
  for (int i = 0; i < 6; ++i) {
    if (dice1.getNum(i) != dice2.getNum(i)) {
      dice1.roll(W);
      dice1.roll(W);
      return false;
    }
  }
  return true;
}

int main() {
  int n;

  cin >> n;

  Dice *dice = new Dice[n];

  for (int i = 0; i < n; ++i) {
    dice[i].setNum();
  }

  for (int ix = 0; ix < n - 1; ++ix) {
    for (int ixx = ix + 1; ixx < n; ++ixx) {
      if (!numCheck(dice[ix], dice[ixx])) {
        continue;
      }

      for (int i = 0; i < 3; ++i) {
        for (int k = 0; k < 4; ++k) {
          if (IsEqual(dice[ix], dice[ixx])) {
            cout << "No" << endl;
            while (true) {
            }
            return 0;
          }
          dice[ixx].roll(N);
        }
        if (i == 0)
          dice[ixx].roll(W);
        if (i == 1) {
          dice[ixx].roll(N);
          dice[ixx].roll(W);
        }
      }
    }
  }
  delete[] dice;

  cout << "Yes" << endl;
  while (true) {
  }
  return 0;
}