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

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

struct Dice {
  Dice(vector<int> &dice) : dice_(dice) {
    east_.push_back(4);
    east_.push_back(2);
    east_.push_back(1);
    east_.push_back(6);
    east_.push_back(5);
    east_.push_back(3);
    north_.push_back(2);
    north_.push_back(6);
    north_.push_back(3);
    north_.push_back(4);
    north_.push_back(1);
    north_.push_back(5);
    south_.push_back(5);
    south_.push_back(1);
    south_.push_back(3);
    south_.push_back(4);
    south_.push_back(6);
    south_.push_back(2);
    west_.push_back(3);
    west_.push_back(2);
    west_.push_back(6);
    west_.push_back(1);
    west_.push_back(5);
    west_.push_back(4);
  }

  void East() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[east_[i] - 1];
  }

  void North() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[north_[i] - 1];
  }

  void South() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[south_[i] - 1];
  }

  void West() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[west_[i] - 1];
  }

  void Ans(int x, int y) {
    vector<int> tmpv;
    tmpv.push_back(1);
    tmpv.push_back(2);
    tmpv.push_back(3);
    tmpv.push_back(4);
    tmpv.push_back(5);
    tmpv.push_back(6);
    Dice tmp(tmpv);

    if (x == 2)
      tmp.North();
    else if (x == 5)
      tmp.South();
    else if (x == 3)
      tmp.West();
    else if (x == 4)
      tmp.East();
    else if (x == 6) {
      tmp.North();
      tmp.North();
    }
    while (tmp.dice_[1] != y || tmp.dice_[0] != x) {
      tmp.East();
      tmp.South();
      tmp.West();
    }

    vector<int> xx = dice_;

    for (int i = 0; i < 6; i++) {
      dice_[i] = xx[tmp.dice_[i] - 1];
    }
  }

  bool operator==(const Dice &l) {
    if (this->dice_ == l.dice_)
      return true;
    else
      return false;
  }

  void Print() {
    for (int i = 0; i < 6; i++)
      cout << dice_[i] << " ";
    cout << endl;
  }

  vector<int> dice_;
  vector<int> east_;
  vector<int> north_;
  vector<int> south_;
  vector<int> west_;
};

int main() {
  vector<int> a(6), b(6);
  cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
  cin >> b[0] >> b[1] >> b[2] >> b[3] >> b[4] >> b[5];
  Dice dA(a), dB(b);

  bool flag = false;
  for (int i = 1; i <= 6; i++) {
    for (int j = 1; j <= 6; j++) {
      if (i + j != 7 && i != j) {
        dB.dice_ = b;
        dB.Ans(i, j);
        if (dA == dB)
          flag = true;
      }
      if (flag)
        break;
    }
    if (flag)
      break;
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct Dice {
  Dice(vector<int> &dice) : dice_(dice) {
    east_.push_back(4);
    east_.push_back(2);
    east_.push_back(1);
    east_.push_back(6);
    east_.push_back(5);
    east_.push_back(3);
    north_.push_back(2);
    north_.push_back(6);
    north_.push_back(3);
    north_.push_back(4);
    north_.push_back(1);
    north_.push_back(5);
    south_.push_back(5);
    south_.push_back(1);
    south_.push_back(3);
    south_.push_back(4);
    south_.push_back(6);
    south_.push_back(2);
    west_.push_back(3);
    west_.push_back(2);
    west_.push_back(6);
    west_.push_back(1);
    west_.push_back(5);
    west_.push_back(4);
  }

  void East() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[east_[i] - 1];
  }

  void North() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[north_[i] - 1];
  }

  void South() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[south_[i] - 1];
  }

  void West() {
    vector<int> tmp(dice_);
    for (int i = 0; i < 6; i++)
      dice_[i] = tmp[west_[i] - 1];
  }

  void Ans(int x, int y) {
    if (dice_[2] != x && dice_[3] != x) {
      while (dice_[0] != x)
        North();
    } else {
      while (dice_[0] != x)
        East();
    }
    while (dice_[1] != y || dice_[0] != x) {
      East();
      South();
      West();
    }
  }

  bool operator==(const Dice &l) {
    if (this->dice_ == l.dice_)
      return true;
    else
      return false;
  }

  vector<int> dice_;
  vector<int> east_;
  vector<int> north_;
  vector<int> south_;
  vector<int> west_;
};

int main() {
  vector<int> a(6), b(6);
  while (true) {
    cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
    cin >> b[0] >> b[1] >> b[2] >> b[3] >> b[4] >> b[5];

    int up, bottom;
    for (int i = 0; i < 6; i++)
      if (b[i] == a[0])
        up = i;
    bottom = 5 - up;
    if (bottom == a[1])
      cout << "No" << endl;
    else {
      Dice dA(a), dB(b);
      dB.Ans(a[0], a[1]);
      if (dA == dB)
        cout << "Yes" << endl;
      else
        cout << "No" << endl;
    }
  }
  return 0;
}