Advertisement
Alyks

Untitled

Mar 26th, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Cube {
  9. public:
  10.     string top;
  11.     string bottom;
  12.     string left;
  13.     string right;
  14.     string front;
  15.     string back;
  16.  
  17.     void rotate(int direction) {
  18.         string tmp;
  19.         switch (direction) {
  20.             case 1:
  21.                 tmp = left;
  22.                 left = front;
  23.                 front = right;
  24.                 right = back;
  25.                 back = tmp;
  26.                 break;
  27.             case 2:
  28.                 tmp = top;
  29.                 top = front;
  30.                 front = bottom;
  31.                 bottom = back;
  32.                 back = tmp;
  33.                 break;
  34.             case 3:
  35.                 tmp = top;
  36.                 top = right;
  37.                 right = bottom;
  38.                 bottom = left;
  39.                 left = tmp;
  40.                 break;
  41.         }
  42.     }
  43. };
  44.  
  45. Cube createCube(string top, string bottom, string left, string right, string front, string back) {
  46.     Cube cube = Cube();
  47.     cube.top = top;
  48.     cube.bottom = bottom;
  49.     cube.left = left;
  50.     cube.right = right;
  51.     cube.front = front;
  52.     cube.back = back;
  53.     return cube;
  54. }
  55.  
  56. bool allDistinct(vector<string> arr) {
  57.     set<string> arr2(begin(arr), end(arr));
  58.     return arr2.size() == arr.size();
  59. }
  60.  
  61. bool validate(vector<Cube> &cubes) {
  62.     vector<string> frontArr;
  63.     vector<string> topArr;
  64.     vector<string> backArr;
  65.     vector<string> bottomArr;
  66.  
  67.     for (Cube cube : cubes) {
  68.         frontArr.push_back(cube.front);
  69.         topArr.push_back(cube.top);
  70.         backArr.push_back(cube.back);
  71.         bottomArr.push_back(cube.bottom);
  72.     }
  73.     return allDistinct(frontArr) && allDistinct(topArr) && allDistinct(backArr) && allDistinct(bottomArr);
  74. }
  75.  
  76. void permute(vector<Cube> &cubes, int cubeNumber, int mistakes, int i, int j, int k) {
  77.     vector<Cube> subcubes;
  78.     for (int n = 0; n < cubeNumber; n++) {
  79.         subcubes.push_back(cubes[n]);
  80.     }
  81.     bool stop = false;
  82.  
  83.     if (validate(subcubes)) {
  84.         if (cubeNumber < 4)
  85.             permute(cubes, cubeNumber + 1, mistakes, 0, 0, 0);
  86.     } else {
  87.         if (i < 5)
  88.             cubes[cubeNumber - 1].rotate(1);
  89.         else if (j < 5) {
  90.             i = 0;
  91.             cubes[cubeNumber - 1].rotate(2);
  92.             j++;
  93.         } else if (k < 5) {
  94.             i = 0;
  95.             j = 0;
  96.             cubes[cubeNumber - 1].rotate(3);
  97.             k++;
  98.         } else {
  99.             if (mistakes < 4) {
  100.                 mistakes++;
  101.                 cubes[0].rotate(2);
  102.             } else {
  103.                 mistakes = 0;
  104.                 cubes[0].rotate(3);
  105.             }
  106.             permute(cubes, 2, mistakes, 0, 0, 0);
  107.             stop = true;
  108.         }
  109.         if (!stop) {
  110.             i++;
  111.             permute(cubes, cubeNumber, mistakes, i, j, k);
  112.         }
  113.     }
  114. }
  115.  
  116. string getSolution(vector<Cube> &cubes) {
  117.     int cubeNum = 1;
  118.     string res = "";
  119.     for (Cube &cube : cubes) {
  120.         res += "\nРешение для кубика №" + to_string(cubeNum) + '\n';
  121.         res += "top = " + cube.top + '\n';
  122.         res += "bottom = " + cube.bottom + '\n';
  123.         res += "left = " + cube.left + '\n';
  124.         res += "right = " + cube.right + '\n';
  125.         res += "front = " + cube.front + '\n';
  126.         res += "back = " + cube.back + '\n';
  127.         cubeNum++;
  128.     }
  129.     return res;
  130. }
  131.  
  132. bool isUserWantToSave() {
  133.     bool notCorrect = true;
  134.     string choice;
  135.     cout << "Хотите сохранить результат в файл?" << endl;
  136.     while(notCorrect) {
  137.         cout << "Введите либо [д], либо [н]" << endl;
  138.         getline(cin, choice);
  139.         if(choice == "д" || choice == "н")
  140.             notCorrect = false;
  141.     }
  142.     return choice == "д";
  143. }
  144.  
  145. void saveResult(string res) {
  146.     if(isUserWantToSave()) {
  147.         cout << "Введите полное имя файла" << endl;
  148.         string filePath;
  149.         getline(cin, filePath);
  150.         ofstream outputFile(filePath);
  151.         if(outputFile.is_open()) {
  152.             outputFile << res;
  153.             cout << "Результат успешно сохранен в файл." << endl;
  154.         } else
  155.             cout << "Произошла ошибка при попытке сохранить данные в файл." << endl;
  156.     }
  157. }
  158.  
  159. int main() {
  160.     cout
  161.             << "Данная программа решает головоломку с кубиками, меняя стороны кубиков таким образом, чтобы они образовывали прямоугольную призму, каждая боковая грань которой раскрашена во все четыре цвета."
  162.             << endl;
  163.     Cube cube1 = createCube("голубой", "красный", "зеленый", "зеленый", "белый", "голубой");
  164.     Cube cube2 = createCube("зеленый", "красный", "красный", "красный", "белый", "голубой");
  165.     Cube cube3 = createCube("зеленый", "красный", "белый", "голубой", "белый", "зеленый");
  166.     Cube cube4 = createCube("белый", "зеленый", "красный", "голубой", "белый", "голубой");
  167.     vector<Cube> cubes = {cube1, cube2, cube3, cube4};
  168.     permute(cubes, 2, 1, 0, 0, 0);
  169.     string solution = getSolution(cubes);
  170.     cout << solution << endl;
  171.     saveResult(solution);
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement