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

#include <iostream>
#include <string>

#define MAX_LEN (100)

using namespace std;

class Dice {
private:
  int value[6];
  int top;

public:
  Dice(int value[]) {
    for (int i = 0; i < 6; i++) {
      this->value[i] = value[i];
    }

    this->top = value[0];
  }

  void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
  }

  void move_N() {
    this->swap(this->value[0], this->value[1]);
    this->swap(this->value[1], this->value[5]);
    this->swap(this->value[5], this->value[4]);

    this->top = this->value[0];
  }

  void move_E() {
    this->swap(value[0], value[3]);
    this->swap(value[3], value[5]);
    this->swap(value[5], value[2]);

    this->top = this->value[0];
  }

  void move_W() {
    this->swap(this->value[0], this->value[2]);
    this->swap(this->value[2], this->value[5]);
    this->swap(this->value[5], this->value[3]);

    this->top = this->value[0];
  }

  void move_S() {
    this->swap(this->value[0], this->value[4]);
    this->swap(this->value[4], this->value[5]);
    this->swap(this->value[5], this->value[1]);

    this->top = this->value[0];
  }

  int Get_top() { return (this->top); }
};

int main() {
  int value[6];
  string inst;

  for (int i = 0; i < 6; i++) {
    do {
      cin >> value[i];
    } while (value[i] < 0 || value[i] > 100);
  }

  Dice dice(value);

  do {
    cin >> inst;
  } while (inst.length() < 0 || inst.length() > MAX_LEN);

  for (int i = 0; inst[i] != '\\0'; i++) {
    if (inst[i] == 'N') {
      dice.move_N();
    } else if (inst[i] == 'E') {
      dice.move_E();
    } else if (inst[i] == 'W') {
      dice.move_W();
    } else if (inst[i] == 'S') {
      dice.move_S();
    }
  }

  cout << dice.Get_top() << endl;

  return (0);
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

class Dice {
private:
  int value[6];
  int top;

public:
  Dice(int value[]) {
    for (int i = 0; i < 6; i++) {
      this->value[i] = value[i];
    }

    this->top = value[0];
  }

  void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
  }

  void move_N(int value[]) {
    this->swap(value[0], value[1]);
    this->swap(value[1], value[5]);
    this->swap(value[5], value[4]);

    this->top = value[0];
  }

  void move_E(int value[]) {
    this->swap(value[0], value[3]);
    this->swap(value[3], value[5]);
    this->swap(value[5], value[2]);

    this->top = value[0];
  }

  void move_W(int value[]) {
    this->swap(value[0], value[2]);
    this->swap(value[2], value[5]);
    this->swap(value[5], value[3]);

    this->top = value[0];
  }

  void move_S(int value[]) {
    this->swap(value[0], value[4]);
    this->swap(value[4], value[5]);
    this->swap(value[5], value[1]);

    this->top = value[0];
  }

  int Get_value(int i) { return (this->value[i]); }
};

int same_dice(Dice dice1, Dice dice2);

int main() {
  int i, j;
  int value[2][6];

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 6; j++) {
      do {
        cin >> value[i][j];
      } while (value[i][j] < 0 || value[i][j] > 100);
    }
  }

  Dice dice1(value[0]);
  Dice dice2(value[1]);

  for (i = 0; i < 10000; i++) {
    srand(time(NULL));

    if (rand() % 4 == 0) {
      dice2.move_N(value[1]);
    } else if (rand() % 4 == 1) {
      dice2.move_E(value[1]);
    } else if (rand() % 4 == 2) {
      dice2.move_W(value[1]);
    } else if (rand() % 4 == 3) {
      dice2.move_S(value[1]);
    }

    if (same_dice(dice1, dice2) == 1) {
      break;
    }
  }

  if (i != 10000) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return (0);
}

int same_dice(Dice dice1, Dice dice2) {
  int i;

  for (i = 0; i < 6; i++) {
    if (dice1.Get_value(i) != dice2.Get_value(i)) {
      break;
    }
  }

  if (i == 6) {
    return (1);
  } else {
    return (0);
  }
}