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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cctype>
#include <fstream>
#include <locale>
#include <math.h>
#include <numeric>
#include <regex>
#include <stdlib.h>

// #include <map>

using namespace std;

class cDise {

  vector<int> X;
  vector<int> Y;
  vector<int> vLabel;

  void rotateDice(char d) {
    int t;

    switch (d) {
    case 'N':
      t = X[0];
      X[0] = Y[0] = Y[1];
      Y[1] = 7 - t;
      break;
    case 'S':
      t = Y[1];
      Y[1] = Y[0];
      X[0] = Y[0] = 7 - t;
      break;
    case 'W':
      t = X[0];
      X[0] = Y[0] = X[1];
      X[1] = 7 - t;
      break;
    case 'E':
      t = X[1];
      X[1] = X[0];
      X[0] = Y[0] = 7 - t;
      break;
    default:
      break;
    }
  }

public:
  cDise(string l) {
    X = {1, 3};
    Y = {1, 2};

    istringstream iss(l);
    while (!iss.eof()) {
      int t;
      iss >> t;
      vLabel.push_back(t);
      iss.ignore();
    }
  }

  void rotateByStr(string dir) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      int t;
      rotateDice(*it);
    }
  }

  void rotateAndFindTop(string dir, int t) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      rotateDice(*it);
      if (getCurrentTop() == t)
        break;
    }
  }

  int getCurrentTop() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[0] - 1);
    }
    return -1;
  }

  int getCurrentFront() {
    if (X[0] == Y[0]) {
      return vLabel.at(Y[1] - 1);
    }
    return -1;
  }

  int getCurrentRight() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[1] - 1);
    }
    return -1;
  }
};

int main() {
  string lb;
  int q;
  getline(cin, lb);

  cDise cd(lb);
  cin >> q;
  cin.ignore();

  for (int i = 0; i < q; i++) {
    string qstr;
    int top, front;

    getline(cin, qstr);
    istringstream iss(qstr);
    iss >> top;
    iss.ignore();
    iss >> front;
    iss.ignore();

    while (cd.getCurrentTop() != top) {
      cd.rotateAndFindTop("NNNNENNN", top);
    }

    while (cd.getCurrentFront() != front) {
      cd.rotateByStr("NES");
    }
    cout << cd.getCurrentRight() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cctype>
#include <fstream>
#include <locale>
#include <math.h>
#include <numeric>
#include <regex>
#include <stdlib.h>

// #include <map>

using namespace std;

class cDise {

  vector<int> X;
  vector<int> Y;
  vector<int> vLabel;

public:
  cDise(string l) {
    X = {1, 3};
    Y = {1, 2};

    istringstream iss(l);
    while (!iss.eof()) {
      int t;
      iss >> t;
      vLabel.push_back(t);
      iss.ignore();
    }
  }

  void rotateDise(string dir) {
    istringstream iss(dir);
    vector<char> vc;
    int char_size = 1;

    for (string::iterator it = dir.begin(); it != dir.end(); it += char_size) {
      // iss.ignore();

      int t;

      switch (*it) {
      case 'N':
        t = X[0];
        X[0] = Y[0] = Y[1];
        Y[1] = 7 - t;
        break;
      case 'S':
        t = Y[1];
        Y[1] = Y[0];
        X[0] = Y[0] = 7 - t;
        break;
      case 'W':
        t = X[0];
        X[0] = Y[0] = X[1];
        X[1] = 7 - t;
        break;
      case 'E':
        t = X[1];
        X[1] = X[0];
        X[0] = Y[0] = 7 - t;
        break;
      default:
        break;
      }
    }
  }

  int getCurrentTop() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[0] - 1);
    }
    return -1;
  }

  int getCurrentFront() {
    if (X[0] == Y[0]) {
      return vLabel.at(Y[1] - 1);
    }
    return -1;
  }

  int getCurrentRight() {
    if (X[0] == Y[0]) {
      return vLabel.at(X[1] - 1);
    }
    return -1;
  }
};

int main() {
  string lb;
  int q;
  getline(cin, lb);

  cDise cd(lb);
  cin >> q;
  cin.ignore();

  for (int i = 0; i < q; i++) {
    string qstr;
    int top, front;

    getline(cin, qstr);
    istringstream iss(qstr);
    iss >> top;
    iss.ignore();
    iss >> front;
    iss.ignore();

    while (cd.getCurrentTop() != top) {
      cd.rotateDise("N");
    }

    while (cd.getCurrentFront() != front) {
      cd.rotateDise("NES");
    }
    cout << cd.getCurrentRight() << endl;
  }
  return 0;
}