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

#include <iostream>
using namespace std;

// サイコロ1
class Dise1 {
public:
  // コンストラクタ
  Dise1();
  int Number[6];
};

// サイコロ2
class Dise2 {
public:
  Dise2();
  int Number[6];
};

Dise1::Dise1() {
  for (int i = 0; i < 6; i++) {
    cin >> Number[i];
  }
}

Dise2::Dise2() {
  for (int i = 0; i < 6; i++) {
    cin >> Number[i];
  }
}

/* 1
  4235
   6*/

// 複数の回転
void Rotation1(Dise1 *dise1, Dise2 *dise2) {
  do {
    swap(dise2->Number[0], dise2->Number[4]);
    swap(dise2->Number[0], dise2->Number[5]);
    swap(dise2->Number[0], dise2->Number[1]);
  } while (dise1->Number[0] != dise2->Number[0]);
}

void Rotation2(Dise1 *dise1, Dise2 *dise2) {
  do {
    swap(dise2->Number[0], dise2->Number[3]);
    swap(dise2->Number[0], dise2->Number[5]);
    swap(dise2->Number[0], dise2->Number[2]);
  } while (dise1->Number[0] != dise2->Number[0]);
}

void Rotation3(Dise1 *dise1, Dise2 *dise2) {
  do {
    swap(dise2->Number[1], dise2->Number[3]);
    swap(dise2->Number[1], dise2->Number[4]);
    swap(dise2->Number[1], dise2->Number[2]);
  } while (dise2->Number[1] != dise1->Number[1]);
}

bool CheckDise(Dise1 *dise1, Dise2 *dise2) {
  for (int i = 0; i < 6; i++) {
    if (dise1->Number[i] != dise2->Number[i]) {
      return false;
    }
  }

  return true;
}

int main() {
  Dise1 dise1;
  Dise2 dise2;

  for (int i = 0; i < 6; i++) {
    // まず上合わせ
    if (dise2.Number[i] == dise1.Number[0]) {
      if (i == 2 || i == 3) {
        Rotation2(&dise1, &dise2);
      } else if (i != 0) {
        Rotation1(&dise1, &dise2);
      }
      // まえ
      for (int j = 1; j < 5; j++) {
        if (dise2.Number[j] == dise1.Number[1]) {
          if (j != 1) {
            Rotation3(&dise1, &dise2);
          }
        }
      }
    }

    if (CheckDise(&dise1, &dise2)) {
      cout << "Yes" << endl;
      exit(0);
    }
  }

  cout << "No" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

// サイコロ1
class Dise1 {
public:
  // コンストラクタ
  Dise1();
  int Number[6];
};

// サイコロ2
class Dise2 {
public:
  Dise2();
  int Number[6];
};

/* 1
  4235
   6*/

Dise1::Dise1() {
  for (int i = 0; i < 6; i++) {
    cin >> Number[i];
  }
}

Dise2::Dise2() {
  for (int i = 0; i < 6; i++) {
    cin >> Number[i];
  }
}

void LengthRotation(Dise1 *dise1, Dise2 *dise2) {
  if (dise2->Number[0] != dise1->Number[0]) {
    // 縦に回す
    do {
      swap(dise2->Number[0], dise2->Number[4]);
      swap(dise2->Number[0], dise2->Number[5]);
      swap(dise2->Number[0], dise2->Number[1]);
    } while (dise2->Number[0] != dise1->Number[0]);

    // まだ違うなら横
    if (dise2->Number[0] != dise1->Number[0]) {
      do {
        swap(dise2->Number[0], dise2->Number[3]);
        swap(dise2->Number[0], dise2->Number[5]);
        swap(dise2->Number[0], dise2->Number[2]);
      } while (dise2->Number[0] != dise1->Number[0]);
    }
  }
}

void SideRotation(Dise1 *dise1, Dise2 *dise2) {
  if (dise2->Number[1] != dise1->Number[1]) {
    do {
      swap(dise2->Number[1], dise2->Number[3]);
      swap(dise2->Number[1], dise2->Number[4]);
      swap(dise2->Number[1], dise2->Number[2]);
    } while (dise2->Number[1] != dise1->Number[1]);
  }
}

bool CheckDise(Dise1 *dise1, Dise2 *dise2) {
  for (int i = 0; i < 6; i++) {
    if (dise1->Number[i] != dise2->Number[i]) {
      return false;
    }
  }

  return true;
}

int main() {
  Dise1 dise1;
  Dise2 dise2;

  // 1を基準
  LengthRotation(&dise1, &dise2);
  SideRotation(&dise1, &dise2);

  if (CheckDise(&dise1, &dise2)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}