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

#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

#define NUM_S 6
#define SIZE 100

using namespace std;

class Dice {
public:
  Dice(const int n[NUM_S]);

  int roll(char);
  int rollF();
  int rollR();
  int rollL();
  int rollB();
  int rollCwise();

  int getTop();
  int getF();
  int getR();
  int getL();
  int getB();
  int getBottom();

  void show();

private:
  int top, front, right, left, back, bottom;
};

bool sameDice(Dice, Dice);
bool checkDice(Dice, Dice);
bool inDice(int, Dice);

int main(void) {
  int n, s[SIZE][NUM_S];
  vector<Dice> d;
  bool different = true;

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < NUM_S; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    d.emplace_back(s[i]);
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (checkDice(d[i], d[j])) {
        different = false;
        break;
      }
    }
  }

  if (different) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
  return 0;
}

Dice::Dice(const int n[NUM_S]) {
  top = n[0];
  front = n[1];
  right = n[2];
  left = n[3];
  back = n[4];
  bottom = n[5];
}

int Dice::roll(char c) {
  if (c == 'S') {
    return rollF();
  } else if (c == 'E') {
    return rollR();
  } else if (c == 'W') {
    return rollL();
  } else if (c == 'N') {
    return rollB();
  } else {
    return 0;
  }
}

int Dice::rollF() {
  int buf = top;
  top = back;
  back = bottom;
  bottom = front;
  front = buf;
  return top;
}

int Dice::rollR() {
  int buf = top;
  top = left;
  left = bottom;
  bottom = right;
  right = buf;
  return top;
}

int Dice::rollL() {
  int buf = top;
  top = right;
  right = bottom;
  bottom = left;
  left = buf;
  return top;
}

int Dice::rollB() {
  int buf = top;
  top = front;
  front = bottom;
  bottom = back;
  back = buf;
  return top;
}

int Dice::rollCwise() {
  int buf = left;
  left = front;
  front = right;
  right = back;
  back = buf;
  return top;
}

int Dice::getTop() { return top; }

int Dice::getF() { return front; }

int Dice::getR() { return right; }

int Dice::getL() { return left; }

int Dice::getB() { return back; }

int Dice::getBottom() { return bottom; }

void Dice::show() {
  cout << "Top:" << top << "\\n";
  cout << "Front:" << front << "\\n";
  cout << "Right:" << right << "\\n";
  cout << "Left:" << left << "\\n";
  cout << "Back:" << back << "\\n";
  cout << "Bottom:" << bottom << "\\n";
  cout << "===\\n";
}

bool sameDice(Dice d1, Dice d2) {
  if (d1.getTop() == d2.getTop() && d1.getF() == d2.getF() &&
      d1.getR() == d2.getR() && d1.getL() == d2.getL() &&
      d1.getB() == d2.getB() && d1.getBottom() == d2.getBottom()) {
    return true;
  } else {
    return false;
  }
}

bool checkDice(Dice d1, Dice d2) {
  for (int j = 0; j < 4; j++) {
    for (int k = 0; k < 4; k++) {
      for (int l = 0; l < 4; l++) {
        if (sameDice(d1, d2)) {
          return true;
        }
        d2.rollR();
      }
      d2.rollF();
    }
    d2.rollCwise();
  }
  return false;
}

bool inDice(int n, Dice d) {
  if (n == d.getTop() || n == d.getF() || n == d.getR() || n == d.getL() ||
      n == d.getB() || n == d.getBottom()) {
    return true;
  } else {
    return false;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

#define NUM_S 6
#define SIZE 100

using namespace std;

class Dice {
public:
  Dice(const int n[NUM_S]);

  int roll(char);
  int rollF();
  int rollR();
  int rollL();
  int rollB();
  int rollCwise();

  int getTop();
  int getF();
  int getR();
  int getL();
  int getB();
  int getBottom();

  void show();

private:
  int top, front, right, left, back, bottom;
};

bool sameDice(Dice, Dice);

int main(void) {
  int n, s[SIZE][NUM_S];
  vector<Dice> d;
  bool same = false;

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < NUM_S; j++) {
      cin >> s[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    d.emplace_back(s[i]);
  }

  for (int i = 1; i < n; i++) {
    if (d[i].getR() == d[0].getTop() || d[i].getL() == d[0].getTop()) {
      d[i].rollR();
    }
    while (d[i].getTop() != d[0].getTop()) {
      d[i].rollF();
    }
    while (d[i].getR() != d[0].getR()) {
      d[i].rollCwise();
    }
    if (sameDice(d[0], d[i])) {
      same = true;
      break;
    }
  }

  if (same) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
  }
  return 0;
}

Dice::Dice(const int n[NUM_S]) {
  top = n[0];
  front = n[1];
  right = n[2];
  left = n[3];
  back = n[4];
  bottom = n[5];
}

int Dice::roll(char c) {
  if (c == 'S') {
    return rollF();
  } else if (c == 'E') {
    return rollR();
  } else if (c == 'W') {
    return rollL();
  } else if (c == 'N') {
    return rollB();
  } else {
    return 0;
  }
}

int Dice::rollF() {
  int buf = top;
  top = back;
  back = bottom;
  bottom = front;
  front = buf;
  return top;
}

int Dice::rollR() {
  int buf = top;
  top = left;
  left = bottom;
  bottom = right;
  right = buf;
  return top;
}

int Dice::rollL() {
  int buf = top;
  top = right;
  right = bottom;
  bottom = left;
  left = buf;
  return top;
}

int Dice::rollB() {
  int buf = top;
  top = front;
  front = bottom;
  bottom = back;
  back = buf;
  return top;
}

int Dice::rollCwise() {
  int buf = left;
  left = front;
  front = right;
  right = back;
  back = buf;
  return top;
}

int Dice::getTop() { return top; }

int Dice::getF() { return front; }

int Dice::getR() { return right; }

int Dice::getL() { return left; }

int Dice::getB() { return back; }

int Dice::getBottom() { return bottom; }

void Dice::show() {
  cout << "Top:" << top << "\\n";
  cout << "Front:" << front << "\\n";
  cout << "Right:" << right << "\\n";
  cout << "Left:" << left << "\\n";
  cout << "Back:" << back << "\\n";
  cout << "Bottom:" << bottom << "\\n";
  cout << "===\\n";
}

bool sameDice(Dice d1, Dice d2) {
  if (d1.getTop() == d2.getTop() && d1.getF() == d2.getF() &&
      d1.getR() == d2.getR() && d1.getL() == d2.getL() &&
      d1.getB() == d2.getB() && d1.getBottom() == d2.getBottom()) {
    return true;
  } else {
    return false;
  }
}