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

#include <iostream>
using namespace std;

class Dice {
public:
  int face[7];

  int center;
  int top;
  int bottom;
  int left;
  int right;
  int back;

  Dice(int num[]) {
    for (int i = 1; i <= 6; i++) {
      face[i] = num[i];
    }
    init();
  }

  void init() {
    center = 1;
    top = 5;
    bottom = 2;
    left = 4;
    right = 3;
    back = 6;
  }

  void move(char command) {
    // cout << command << endl;
    int tmp;
    switch (command) {
    case 'E':
      // cout << center << top << bottom << left << right << back << endl;

      top = top;       // 変化せず
      bottom = bottom; // 変化せず
      tmp = right;
      right = center;
      center = left;
      left = back;
      back = tmp;
      // printDebug();

      // cout << center << top << bottom << left << right << back << endl;
      break;
    case 'N':
      left = left;   // 変化せず
      right = right; // 変化せず
      tmp = top;
      top = center;
      center = bottom;
      bottom = back;
      back = tmp;
      // printDebug();
      break;
    case 'S':
      left = left;   // 変化せず
      right = right; // 変化せず
      tmp = bottom;
      bottom = center;
      center = top;
      top = back;
      back = tmp;
      // printDebug();
      break;
    case 'W':
      top = top;       // 変化せず
      bottom = bottom; // 変化せず
      tmp = left;
      left = center;
      center = right;
      right = back;
      back = tmp;
      // printDebug();
      break;
    }
  }

  void rotateRight() {
    int tmp = top;
    top = right;
    right = bottom;
    bottom = left;
    left = tmp;
  }

  // void setCenter(int _center){
  void setCenter(int _no) {
    // // cout << _center << endl;
    // int diceIndex = 0;
    // for(int i = 1; i <= 6; i++){
    //     if(_center == face[i]){
    //         diceIndex = i;
    //         break;
    //     }
    // }
    // // cout << diceIndex << endl;

    // switch(diceIndex){
    switch (_no) {
    case 1:
      // 何もしない
      break;
    case 2:
      move('N');
      break;
    case 3:
      move('W');
      break;
    case 4:
      move('E');
      break;
    case 5:
      move('S');
      break;
    case 6:
      move('S');
      move('S');
      break;
    }
  }
  void setBottom(int _bottom) {
    // cout << _bottom << endl;
    int diceIndex = 0;
    for (int i = 1; i <= 6; i++) {
      if (_bottom == face[i]) {
        diceIndex = i;
        break;
      }
    }
    // cout << diceIndex << endl;
    while (bottom != diceIndex) {
      rotateRight();
    }
  }

  void printDebug() {
    // cout << "===============================" << endl;
    // cout << "center: " << face[center] << endl;
    // cout << "top: " <<  face[top] << endl;
    // cout << "bottom: " <<  face[bottom] << endl;
    // cout << "left: " <<  face[left] << endl;
    // cout << "right: " <<  face[right] << endl;
    // cout << "back: " <<  face[back] << endl;
    // cout << "===============================" << endl;
    cout << face[center] << face[bottom] << face[right] << face[left]
         << face[top] << face[back] << endl;
  }

  void print() { cout << face[right] << endl; }
};

bool isSameDices(Dice diceA, Dice diceB) {
  // void isSameDices(Dice diceA, Dice diceB){
  if (diceA.face[diceA.center] != diceB.face[diceB.center]) {
    return false;
    // cout << "No" << endl;
    // return;
  }
  if (diceA.face[diceA.top] != diceB.face[diceB.top]) {
    return false;
    // cout << "No" << endl;
    // return;
  }
  if (diceA.face[diceA.bottom] != diceB.face[diceB.bottom]) {
    return false;
    // cout << "No" << endl;
    // return;
  }
  if (diceA.face[diceA.right] != diceB.face[diceB.right]) {
    return false;
    // cout << "No" << endl;
    // return;
  }
  if (diceA.face[diceA.left] != diceB.face[diceB.left]) {
    return false;
    // cout << "No" << endl;
    // return;
  }
  if (diceA.face[diceA.back] != diceB.face[diceB.back]) {
    return false;
    // cout << "No" << endl;
    // return;
  }

  return true;
  // cout << "Yes" << endl;
  // return;
}

int main() {
  int num;
  int arrayA[7];
  int arrayB[7];

  for (int i = 1; i <= 6; i++) {
    cin >> num;
    arrayA[i] = num;
  }
  Dice dice1(arrayA);
  // dice1.setCenter(arrayA[1]);
  // dice1.setBottom(arrayA[2]);
  // dice1.printDebug();

  for (int i = 1; i <= 6; i++) {
    cin >> num;
    arrayB[i] = num;
  }
  Dice dice2(arrayB);
  // dice2.setCenter(arrayA[1]);
  // dice2.setBottom(arrayA[2]);
  // dice2.printDebug();

  // cout << arrayA[dice1.right] << endl;
  // cout << arrayB[dice2.right] << endl;

  // if(arrayA[dice1.right] == arrayB[dice2.right]){
  //     cout << "Yes" << endl;
  // }else{
  //     cout << "No" << endl;
  // }

  for (int i = 1; i <= 6; i++) {
    // dice2.printDebug();
    // cout << i << endl;
    dice2.setCenter(i);
    for (int j = 1; j <= 4; j++) {
      dice2.rotateRight();
      // dice2.printDebug();
      // isSameDices(dice1, dice2);
      if (isSameDices(dice1, dice2)) {
        cout << "Yes" << endl;
        return 0;
      }
    }
    dice2.init();
  }

  cout << "No" << endl;

  // if(isSameDices(dice1, dice2)){
  //     cout << "Yes" << endl;
  // } else {
  //     cout << "No" << endl;
  // }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Dice {
public:
  int face[7];

  int center;
  int top;
  int bottom;
  int left;
  int right;

  Dice(int num[]) {
    for (int i = 1; i <= 6; i++) {
      face[i] = num[i];
    }

    center = 1;
    top = 5;
    bottom = 2;
    left = 4;
    right = 3;
  }

  void move(char command) {
    switch (command) {
    case 'E':
      top = top;       // 変化せず
      bottom = bottom; // 変化せず
      right = center;
      center = left;
      left = 7 - right;
      // printDebug();
      break;
    case 'N':
      left = left;   // 変化せず
      right = right; // 変化せず
      top = center;
      center = bottom;
      bottom = 7 - top;
      // printDebug();
      break;
    case 'S':
      left = left;   // 変化せず
      right = right; // 変化せず
      bottom = center;
      center = top;
      top = 7 - bottom;
      // printDebug();
      break;
    case 'W':
      top = top;       // 変化せず
      bottom = bottom; // 変化せず
      left = center;
      center = right;
      right = 7 - left;
      // printDebug();
      break;
    }
  }

  void rotateRight() {
    top = right;
    right = bottom;
    bottom = left;
    left = 7 - right;
  }

  void setCenter(int _center) {
    // cout << _center << endl;
    int diceIndex = 0;
    for (int i = 1; i <= 6; i++) {
      if (_center == face[i]) {
        diceIndex = i;
        break;
      }
    }
    // cout << diceIndex << endl;

    switch (diceIndex) {
    case 1:
      // 何もしない
      break;
    case 2:
      move('N');
      break;
    case 3:
      move('W');
      break;
    case 4:
      move('E');
      break;
    case 5:
      move('S');
      break;
    case 6:
      move('S');
      move('S');
      break;
    }
  }
  void setBottom(int _bottom) {
    // cout << _bottom << endl;
    int diceIndex = 0;
    for (int i = 1; i <= 6; i++) {
      if (_bottom == face[i]) {
        diceIndex = i;
        break;
      }
    }
    // cout << diceIndex << endl;
    while (bottom != diceIndex) {
      rotateRight();
    }
  }

  void printDebug() {
    cout << "===============================" << endl;
    cout << "center: " << face[center] << endl;
    cout << "top: " << face[top] << endl;
    cout << "bottom: " << face[bottom] << endl;
    cout << "left: " << face[left] << endl;
    cout << "right: " << face[right] << endl;
    cout << "===============================" << endl;
  }

  void print() { cout << face[right] << endl; }
};

bool isSameDices(Dice diceA, Dice diceB) {
  if (diceA.face[diceA.center] != diceB.face[diceB.center]) {
    return false;
  }
  if (diceA.face[diceA.top] != diceB.face[diceB.top]) {
    return false;
  }
  if (diceA.face[diceA.bottom] != diceB.face[diceB.bottom]) {
    return false;
  }
  if (diceA.face[diceA.right] != diceB.face[diceB.right]) {
    return false;
  }
  if (diceA.face[diceA.left] != diceB.face[diceB.left]) {
    return false;
  }

  return true;
}

int main() {
  int num;
  int arrayA[7];
  int arrayB[7];

  for (int i = 1; i <= 6; i++) {
    cin >> num;
    arrayA[i] = num;
  }
  Dice dice1(arrayA);
  dice1.setCenter(arrayA[1]);
  dice1.setBottom(arrayA[2]);
  // dice1.printDebug();

  for (int i = 1; i <= 6; i++) {
    cin >> num;
    arrayB[i] = num;
  }
  Dice dice2(arrayB);
  dice2.setCenter(arrayA[1]);
  dice2.setBottom(arrayA[2]);
  // dice2.printDebug();

  // cout << arrayA[dice1.right] << endl;
  // cout << arrayB[dice2.right] << endl;

  // if(arrayA[dice1.right] == arrayB[dice2.right]){
  //     cout << "Yes" << endl;
  // }else{
  //     cout << "No" << endl;
  // }

  if (isSameDices(dice1, dice2)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}