fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Face {
  5. vector<vector<string>> grid;
  6. };
  7.  
  8. void rotateRight(Face &f) {
  9. int n = f.grid.size();
  10. vector<vector<string>> res(n, vector<string>(n));
  11. for (int i = 0; i < n; ++i)
  12. for (int j = 0; j < n; ++j)
  13. res[j][n - 1 - i] = f.grid[i][j];
  14. f.grid = res;
  15. }
  16.  
  17. void rotateLeft(Face &f) {
  18. int n = f.grid.size();
  19. vector<vector<string>> res(n, vector<string>(n));
  20. for (int i = 0; i < n; ++i)
  21. for (int j = 0; j < n; ++j)
  22. res[n - 1 - j][i] = f.grid[i][j];
  23. f.grid = res;
  24. }
  25.  
  26. bool isUniform(Face &f) {
  27. string c = f.grid[0][0];
  28. for (auto &r : f.grid)
  29. for (auto &x : r)
  30. if (x != c) return false;
  31. return true;
  32. }
  33.  
  34. bool anyFaceUniform(vector<Face> &cube) {
  35. for (auto &f : cube)
  36. if (isUniform(f)) return true;
  37. return false;
  38. }
  39.  
  40. void applyInstruction(vector<Face> &cube, const string &instr) {
  41. stringstream ss(instr);
  42. string a, b, c;
  43. ss >> a;
  44. if (a == "turn") {
  45. ss >> b;
  46. if (b == "left") rotateLeft(cube[3]); // front
  47. else if (b == "right") rotateRight(cube[3]);
  48. } else if (a == "rotate") {
  49. ss >> b;
  50. if (b == "front") rotateRight(cube[3]);
  51. else if (b == "back") rotateRight(cube[1]);
  52. else if (b == "left") rotateLeft(cube[4]);
  53. else if (b == "right") rotateRight(cube[5]);
  54. } else {
  55. // side row/col move
  56. ss >> b >> c;
  57. if (a == "top") rotateRight(cube[2]);
  58. else if (a == "base") rotateLeft(cube[0]);
  59. else if (a == "front") rotateRight(cube[3]);
  60. else if (a == "back") rotateLeft(cube[1]);
  61. else if (a == "left") rotateRight(cube[4]);
  62. else if (a == "right") rotateLeft(cube[5]);
  63. }
  64. }
  65.  
  66. bool isFaulty(vector<Face> &cube) {
  67. unordered_map<string, int> freq;
  68. for (auto &f : cube)
  69. for (auto &r : f.grid)
  70. for (auto &x : r)
  71. freq[x]++;
  72. int mn = INT_MAX, mx = INT_MIN;
  73. for (auto &p : freq) {
  74. mn = min(mn, p.second);
  75. mx = max(mx, p.second);
  76. }
  77.  
  78. return (mx - mn > 1);
  79. }
  80.  
  81. int main() {
  82. ios::sync_with_stdio(false);
  83. cin.tie(nullptr);
  84.  
  85. int N, K;
  86. cin >> N >> K;
  87.  
  88. vector<Face> cube(6);
  89. for (int f = 0; f < 6; ++f) {
  90. cube[f].grid.resize(N, vector<string>(N));
  91. for (int i = 0; i < N; ++i)
  92. for (int j = 0; j < N; ++j)
  93. cin >> cube[f].grid[i][j];
  94. }
  95.  
  96. cin.ignore();
  97. vector<string> instructions(K);
  98. for (int i = 0; i < K; ++i) {
  99. getline(cin, instructions[i]);
  100. }
  101.  
  102. bool faulty = isFaulty(cube);
  103. string extra = "";
  104. bool found = false;
  105.  
  106. // Try skipping one instruction at a time
  107. for (int skip = 0; skip < K; ++skip) {
  108. vector<Face> temp = cube;
  109. for (int i = 0; i < K; ++i)
  110. if (i != skip)
  111. applyInstruction(temp, instructions[i]);
  112. if (anyFaceUniform(temp)) {
  113. extra = instructions[skip];
  114. found = true;
  115. break;
  116. }
  117. }
  118.  
  119. if (faulty && found) {
  120. cout << "Faulty\n" << extra << "\n";
  121. } else if (!faulty && found) {
  122. cout << extra << "\n";
  123. } else {
  124. cout << "Not Possible\n";
  125. }
  126.  
  127. return 0;
  128. }
  129.  
Success #stdin #stdout 0.01s 5312KB
stdin
45
stdout