Problem p02386 (C++) — buggy submission, verdict: Runtime Error

/**
 * ???????????? IV

Dice I ??¨????§????????????§?????\\??????????????°????????? n
???????????????????????????????????????????????????????????????????????¨?????°???????????????????????????????????????????????°??????????????????????????????????????????????????????????????????
Dice III ??????????????¨????????????

Input
????????????????????????????????° n ?????????????????????????¶???? n
??????????????????????????¢?????°???????????????????????????

????????????????????????????????¢?????°???????????????????????????????????????????????§??????????????????????????????

Output
??\\??????????????????????????????????????°????????´???
"Yes"??????????????????????????\\?????????????????´??? "No"
??¨????????????????????????????????????

Constraints
2???n???100
0??? ??\\?????????????????????????????¢?????°??? ???100

Sample Input 1
3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1

Sample Output 1
No

Sample Input 2
3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6

Sample Output 2
Yes
 */

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

class Dice {
private:
  // Top,Front,Right,Left,Rear,Bottom;
  enum SurfaceNameIndex { TOP, FRONT, RIGHT, LEFT, REAR, BOTTOM };
  std::string InitialState;
  // std::map<std::string, unsigned short> DiceState;
  std::vector<unsigned short> vInitialState;
  std::vector<unsigned short> vDiceState;

public:
  Dice() : Dice("1 2 3 4 5 6") {}

  explicit Dice(std::string Param)
      : vInitialState{1, 2, 3, 4, 5, 6}, vDiceState{1, 2, 3, 4, 5, 6} {
    setDiceLabels(Param);
  }

  unsigned short getTop();
  unsigned short getLabel(std::string);
  unsigned short getRight(unsigned short, unsigned short);
  void roll(std::string);
  void rotate(std::string);
  void rollToTop(unsigned short Label);
  void rollToFront(unsigned short Label);
  void setDiceLabels(std::string);
  std::string findPosition(unsigned short);
  std::string getDiceLabels();
  std::string getInitialState();
};

unsigned short Dice::getTop() { return vDiceState[TOP]; }
unsigned short Dice::getLabel(std::string SurfaceName) {
  unsigned short Label = 0;
  // Top,Front,Right,Left,Rear,Bottom;
  if (SurfaceName == "Top") {
    Label = vDiceState[TOP];
  } else if (SurfaceName == "Front") {
    Label = vDiceState[FRONT];
  } else if (SurfaceName == "Right") {
    Label = vDiceState[RIGHT];
  } else if (SurfaceName == "Left") {
    Label = vDiceState[LEFT];
  } else if (SurfaceName == "Rear") {
    Label = vDiceState[REAR];
  } else if (SurfaceName == "Bottom") {
    Label = vDiceState[BOTTOM];
  }
  return Label;
}

void Dice::roll(std::string Direction) {
  unsigned short Tmp;
  size_t DirectionCount = Direction.size();
  for (size_t i = 0; i < DirectionCount; i++) {
    char e = Direction[i];

    // for (const auto &e : Direction) {
    if (e == 'N') {
      // Tmp = DiceState["Rear"];
      // DiceState["Rear"] = DiceState["Top"];
      // DiceState["Top"] = DiceState["Front"];
      // DiceState["Front"] = DiceState["Bottom"];
      // DiceState["Bottom"] = Tmp;
      Tmp = vDiceState[REAR];
      vDiceState[REAR] = vDiceState[TOP];
      vDiceState[TOP] = vDiceState[FRONT];
      vDiceState[FRONT] = vDiceState[BOTTOM];
      vDiceState[BOTTOM] = Tmp;
    } else if (e == 'E') {
      // Tmp = DiceState["Right"];
      // DiceState["Right"] = DiceState["Top"];
      // DiceState["Top"] = DiceState["Left"];
      // DiceState["Left"] = DiceState["Bottom"];
      // DiceState["Bottom"] = Tmp;
      Tmp = vDiceState[RIGHT];
      vDiceState[RIGHT] = vDiceState[TOP];
      vDiceState[TOP] = vDiceState[LEFT];
      vDiceState[LEFT] = vDiceState[BOTTOM];
      vDiceState[BOTTOM] = Tmp;
    } else if (e == 'W') {
      // Tmp = DiceState["Left"];
      // DiceState["Left"] = DiceState["Top"];
      // DiceState["Top"] = DiceState["Right"];
      // DiceState["Right"] = DiceState["Bottom"];
      // DiceState["Bottom"] = Tmp;
      Tmp = vDiceState[LEFT];
      vDiceState[LEFT] = vDiceState[TOP];
      vDiceState[TOP] = vDiceState[RIGHT];
      vDiceState[RIGHT] = vDiceState[BOTTOM];
      vDiceState[BOTTOM] = Tmp;
    } else if (e == 'S') {
      // Tmp = DiceState["Front"];
      // DiceState["Front"] = DiceState["Top"];
      // DiceState["Top"] = DiceState["Rear"];
      // DiceState["Rear"] = DiceState["Bottom"];
      // DiceState["Bottom"] = Tmp;
      Tmp = vDiceState[FRONT];
      vDiceState[FRONT] = vDiceState[TOP];
      vDiceState[TOP] = vDiceState[REAR];
      vDiceState[REAR] = vDiceState[BOTTOM];
      vDiceState[BOTTOM] = Tmp;
    }
  }
}

void Dice::rotate(std::string Direction) {
  unsigned short Tmp;
  size_t DirectionCount = Direction.size();
  for (size_t i = 0; i < DirectionCount; i++) {
    char e = Direction[i];
    // for (const auto &e : Direction) {
    if (e == 'R') {
      // Tmp = DiceState["Rear"];
      // DiceState["Rear"] = DiceState["Left"];
      // DiceState["Left"] = DiceState["Front"];
      // DiceState["Front"] = DiceState["Right"];
      // DiceState["Right"] = Tmp;
      Tmp = vDiceState[REAR];
      vDiceState[REAR] = vDiceState[LEFT];
      vDiceState[LEFT] = vDiceState[FRONT];
      vDiceState[FRONT] = vDiceState[RIGHT];
      vDiceState[RIGHT] = Tmp;
    } else if (e == 'L') {
      // Tmp = DiceState["Rear"];
      // DiceState["Rear"] = DiceState["Right"];
      // DiceState["Right"] = DiceState["Front"];
      // DiceState["Front"] = DiceState["Left"];
      // DiceState["Left"] = Tmp;
      Tmp = vDiceState[REAR];
      vDiceState[REAR] = vDiceState[RIGHT];
      vDiceState[RIGHT] = vDiceState[FRONT];
      vDiceState[FRONT] = vDiceState[LEFT];
      vDiceState[LEFT] = Tmp;
    }
  }
}

void Dice::rollToTop(unsigned short Label) {
  std::string StartPosition = findPosition(Label);
  if (StartPosition == "Front") {
    roll("N");
  } else if (StartPosition == "Right") {
    roll("W");
  } else if (StartPosition == "Left") {
    roll("E");
  } else if (StartPosition == "Rear") {
    roll("S");
  } else if (StartPosition == "Bottom") {
    roll("EE");
  }
}

void Dice::rollToFront(unsigned short Label) {
  std::string StartPosition = findPosition(Label);
  if (StartPosition == "Right") {
    roll("WSE");
  } else if (StartPosition == "Left") {
    roll("ESW");
  } else if (StartPosition == "Rear") {
    roll("SSEE");
  } else if (StartPosition == "Top") {
    roll("S");
  } else if (StartPosition == "Bottom") {
    roll("N");
  }
}

void Dice::setDiceLabels(std::string Str) {
  InitialState = Str;
  // vDiceState = {0, 0, 0, 0, 0, 0};
  std::istringstream iss(Str);
  unsigned short n;
  size_t i = 0;

  while (iss >> n) {
    size_t j = i;
    i++;
    switch (j) {
      // Top,Front,Right,Left,Rear,Bottom;
    case 0:
      // DiceState["Top"] = n;
      vDiceState[TOP] = n;
      break;
    case 1:
      // DiceState["Front"] = n;
      vDiceState[FRONT] = n;
      break;
    case 2:
      // DiceState["Right"] = n;
      vDiceState[RIGHT] = n;
      break;
    case 3:
      // DiceState["Left"] = n;
      vDiceState[LEFT] = n;
      break;
    case 4:
      // DiceState["Rear"] = n;
      vDiceState[REAR] = n;
      break;
    case 5:
      // DiceState["Bottom"] = n;
      vDiceState[BOTTOM] = n;
      break;
    default:
      break;
    }
  }
}

unsigned short Dice::getRight(unsigned short Top, unsigned short Front) {
  rollToTop(Top);
  rollToFront(Front);
  return getLabel("Right");
}

std::string Dice::findPosition(unsigned short Label) {
  // auto its =
  //     find_if(DiceState.begin(), DiceState.end(),
  //             [Label](std::map<std::string, unsigned short>::value_type &p) {
  //               return p.second == Label;
  //             });
  // return its->first;
  std::string SurfaceName;
  auto itr = find(vDiceState.begin(), vDiceState.end(), Label);
  size_t i = std::distance(vDiceState.begin(), itr);
  // Top,Front,Right,Left,Rear,Bottom;
  switch (i) {
  case 0:
    SurfaceName = "Top";
    break;
  case 1:
    SurfaceName = "Front";
    break;
  case 2:
    SurfaceName = "Right";
    break;
  case 3:
    SurfaceName = "Left";
    break;
  case 4:
    SurfaceName = "Rear";
    break;
  case 5:
    SurfaceName = "Bottom";
    break;
  }
  return SurfaceName;
}

std::string Dice::getDiceLabels() {
  std::ostringstream oss;

  // oss << DiceState["Top"] << " ";
  // oss << DiceState["Front"] << " ";
  // oss << DiceState["Right"] << " ";
  // oss << DiceState["Left"] << " ";
  // oss << DiceState["Rear"] << " ";
  // oss << DiceState["Bottom"];
  oss << vDiceState[TOP] << " ";
  oss << vDiceState[FRONT] << " ";
  oss << vDiceState[RIGHT] << " ";
  oss << vDiceState[LEFT] << " ";
  oss << vDiceState[REAR] << " ";
  oss << vDiceState[BOTTOM];

  return oss.str();
}

std::string Dice::getInitialState() { return InitialState; }

int main(int argc, char const *argv[]) {
  std::string D1Labels, D2Labels, Param;
  std::istringstream iss;
  unsigned short n;

  getline(std::cin, Param);
  iss.str(Param);
  iss >> n;
  iss.clear();

  getline(std::cin, D1Labels);
  // Top,Front,Right,Left,Rear,Bottom;
  Dice *D1;
  D1 = new Dice(D1Labels);
  unsigned short TrueCount = 0;
  // Dice D2;
  Dice *D2;

  for (size_t i = 0; i < n - 1; i++) {
    getline(std::cin, D2Labels);
    D2 = new Dice;

    D2->setDiceLabels(D2Labels);

    D2->rollToTop(D1->getTop());
    // std::cout << D2.getDiceLabels() << std::endl;
    D2->rollToFront(D1->getLabel("Front"));
    if (D1Labels == D2->getDiceLabels()) {
      TrueCount++;
    } else {
      // for (const auto& e : D2Labels) {
      // std::cout << e << std::endl;
      std::istringstream iss;
      iss.str(D2Labels);
      for (size_t i = 0; i < 6; i++) {
        unsigned short t;
        iss >> t;
        D2->rollToTop(t);
        for (size_t i = 0; i < 4; i++) {
          D2->rotate("R");
          std::string D2tmp = D2->getDiceLabels();
          if (D1Labels == D2tmp) {
            TrueCount++;
            break;
          }
        }
      }
      iss.str("");
      iss.clear();
    }
    delete D2;
  }
  if (TrueCount == 0) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  delete D1;

  return 0;
}

Error: 0

Fixed submission:

/**
 * ???????????? IV

Dice I ??¨????§????????????§?????\\??????????????°????????? n
???????????????????????????????????????????????????????????????????????¨?????°???????????????????????????????????????????????°??????????????????????????????????????????????????????????????????
Dice III ??????????????¨????????????

Input
????????????????????????????????° n ?????????????????????????¶???? n
??????????????????????????¢?????°???????????????????????????

????????????????????????????????¢?????°???????????????????????????????????????????????§??????????????????????????????

Output
??\\??????????????????????????????????????°????????´???
"Yes"??????????????????????????\\?????????????????´??? "No"
??¨????????????????????????????????????

Constraints
2???n???100
0??? ??\\?????????????????????????????¢?????°??? ???100

Sample Input 1
3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1

Sample Output 1
No

Sample Input 2
3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6

Sample Output 2
Yes
 */

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

class Dice {
private:
  std::string InitialState;
  std::map<std::string, unsigned short> DiceState;
  std::vector<unsigned short> vInitialState;
  std::vector<unsigned short> vDiceState;

public:
  Dice() : Dice("1 2 3 4 5 6") {}

  explicit Dice(std::string Param) { setDiceLabels(Param); }

  unsigned short getTop();
  unsigned short getLabel(std::string);
  unsigned short getRight(unsigned short, unsigned short);
  void roll(std::string);
  void rotate(std::string);
  void rollToTop(unsigned short Label);
  void rollToFront(unsigned short Label);
  void setDiceLabels(std::string);
  std::string findPosition(unsigned short);
  std::string getDiceLabels();
  std::string getInitialState();
};

unsigned short Dice::getTop() { return DiceState["Top"]; }
unsigned short Dice::getLabel(std::string SurfaceName) {
  return DiceState[SurfaceName];
}

void Dice::roll(std::string Direction) {
  unsigned short Tmp;
  size_t DirectionCount = Direction.size();
  for (size_t i = 0; i < DirectionCount; i++) {
    char e = Direction[i];

    // for (const auto &e : Direction) {
    if (e == 'N') {
      Tmp = DiceState["Rear"];
      DiceState["Rear"] = DiceState["Top"];
      DiceState["Top"] = DiceState["Front"];
      DiceState["Front"] = DiceState["Bottom"];
      DiceState["Bottom"] = Tmp;
    } else if (e == 'E') {
      Tmp = DiceState["Right"];
      DiceState["Right"] = DiceState["Top"];
      DiceState["Top"] = DiceState["Left"];
      DiceState["Left"] = DiceState["Bottom"];
      DiceState["Bottom"] = Tmp;
    } else if (e == 'W') {
      Tmp = DiceState["Left"];
      DiceState["Left"] = DiceState["Top"];
      DiceState["Top"] = DiceState["Right"];
      DiceState["Right"] = DiceState["Bottom"];
      DiceState["Bottom"] = Tmp;
    } else if (e == 'S') {
      Tmp = DiceState["Front"];
      DiceState["Front"] = DiceState["Top"];
      DiceState["Top"] = DiceState["Rear"];
      DiceState["Rear"] = DiceState["Bottom"];
      DiceState["Bottom"] = Tmp;
    }
  }
}

void Dice::rotate(std::string Direction) {
  unsigned short Tmp;
  size_t DirectionCount = Direction.size();
  for (size_t i = 0; i < DirectionCount; i++) {
    char e = Direction[i];
    // for (const auto &e : Direction) {
    if (e == 'R') {
      Tmp = DiceState["Rear"];
      DiceState["Rear"] = DiceState["Left"];
      DiceState["Left"] = DiceState["Front"];
      DiceState["Front"] = DiceState["Right"];
      DiceState["Right"] = Tmp;
    } else if (e == 'L') {
      Tmp = DiceState["Rear"];
      DiceState["Rear"] = DiceState["Right"];
      DiceState["Right"] = DiceState["Front"];
      DiceState["Front"] = DiceState["Left"];
      DiceState["Left"] = Tmp;
    }
  }
}

void Dice::rollToTop(unsigned short Label) {
  std::string StartPosition = findPosition(Label);
  if (StartPosition == "Front") {
    roll("N");
  } else if (StartPosition == "Right") {
    roll("W");
  } else if (StartPosition == "Left") {
    roll("E");
  } else if (StartPosition == "Rear") {
    roll("S");
  } else if (StartPosition == "Bottom") {
    roll("EE");
  }
}

void Dice::rollToFront(unsigned short Label) {
  std::string StartPosition = findPosition(Label);
  if (StartPosition == "Right") {
    roll("WSE");
  } else if (StartPosition == "Left") {
    roll("ESW");
  } else if (StartPosition == "Rear") {
    roll("SSEE");
  } else if (StartPosition == "Top") {
    roll("S");
  } else if (StartPosition == "Bottom") {
    roll("N");
  }
}

void Dice::setDiceLabels(std::string Str) {
  InitialState = Str;
  std::istringstream iss(Str);
  unsigned short n;
  size_t i = 0;

  while (iss >> n) {
    size_t j = i;
    i++;
    switch (j) {
    case 0:
      DiceState["Top"] = n;
      break;
    case 1:
      DiceState["Front"] = n;
      break;
    case 2:
      DiceState["Right"] = n;
      break;
    case 3:
      DiceState["Left"] = n;
      break;
    case 4:
      DiceState["Rear"] = n;
      break;
    case 5:
      DiceState["Bottom"] = n;
      break;
    default:
      break;
    }
  }
}

unsigned short Dice::getRight(unsigned short Top, unsigned short Front) {
  rollToTop(Top);
  rollToFront(Front);
  return getLabel("Right");
}

std::string Dice::findPosition(unsigned short Label) {
  auto its =
      find_if(DiceState.begin(), DiceState.end(),
              [Label](std::map<std::string, unsigned short>::value_type &p) {
                return p.second == Label;
              });
  return its->first;
}

std::string Dice::getDiceLabels() {
  std::ostringstream oss;

  oss << DiceState["Top"] << " ";
  oss << DiceState["Front"] << " ";
  oss << DiceState["Right"] << " ";
  oss << DiceState["Left"] << " ";
  oss << DiceState["Rear"] << " ";
  oss << DiceState["Bottom"];

  return oss.str();
}

std::string Dice::getInitialState() { return InitialState; }

int main(int argc, char const *argv[]) {
  std::string D1Labels, D2Labels, Param;
  std::istringstream iss;
  unsigned short n;

  getline(std::cin, Param);
  iss.str(Param);
  iss >> n;
  iss.clear();

  getline(std::cin, D1Labels);
  // Top,Front,Right,Left,Rear,Bottom;
  Dice *D1;
  D1 = new Dice(D1Labels);
  unsigned short TrueCount = 0;
  // Dice D2;
  Dice *D2;

  for (size_t i = 0; i < n - 1; i++) {
    getline(std::cin, D2Labels);
    D2 = new Dice;

    D2->setDiceLabels(D2Labels);

    D2->rollToTop(D1->getTop());
    // std::cout << D2.getDiceLabels() << std::endl;
    D2->rollToFront(D1->getLabel("Front"));
    if (D1Labels == D2->getDiceLabels()) {
      TrueCount++;
    } else {
      // for (const auto& e : D2Labels) {
      // std::cout << e << std::endl;
      std::istringstream iss;
      iss.str(D2Labels);
      for (size_t i = 0; i < 6; i++) {
        unsigned short t;
        iss >> t;
        D2->rollToTop(t);
        for (size_t i = 0; i < 4; i++) {
          D2->rotate("R");
          std::string D2tmp = D2->getDiceLabels();
          if (D1Labels == D2tmp) {
            TrueCount++;
            break;
          }
        }
      }
      iss.str("");
      iss.clear();
    }
    delete D2;
  }
  if (TrueCount == 0) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  delete D1;

  return 0;
}