Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <fstream>
- using namespace std;
- struct Cube {
- public:
- string top;
- string bottom;
- string left;
- string right;
- string front;
- string back;
- void rotate(int direction) {
- string tmp;
- switch (direction) {
- case 1:
- tmp = left;
- left = front;
- front = right;
- right = back;
- back = tmp;
- break;
- case 2:
- tmp = top;
- top = front;
- front = bottom;
- bottom = back;
- back = tmp;
- break;
- case 3:
- tmp = top;
- top = right;
- right = bottom;
- bottom = left;
- left = tmp;
- break;
- }
- }
- };
- Cube createCube(string top, string bottom, string left, string right, string front, string back) {
- Cube cube = Cube();
- cube.top = top;
- cube.bottom = bottom;
- cube.left = left;
- cube.right = right;
- cube.front = front;
- cube.back = back;
- return cube;
- }
- bool allDistinct(vector<string> arr) {
- set<string> arr2(begin(arr), end(arr));
- return arr2.size() == arr.size();
- }
- bool validate(vector<Cube> &cubes) {
- vector<string> frontArr;
- vector<string> topArr;
- vector<string> backArr;
- vector<string> bottomArr;
- for (Cube cube : cubes) {
- frontArr.push_back(cube.front);
- topArr.push_back(cube.top);
- backArr.push_back(cube.back);
- bottomArr.push_back(cube.bottom);
- }
- return allDistinct(frontArr) && allDistinct(topArr) && allDistinct(backArr) && allDistinct(bottomArr);
- }
- void permute(vector<Cube> &cubes, int cubeNumber, int mistakes, int i, int j, int k) {
- vector<Cube> subcubes;
- for (int n = 0; n < cubeNumber; n++) {
- subcubes.push_back(cubes[n]);
- }
- bool stop = false;
- if (validate(subcubes)) {
- if (cubeNumber < 4)
- permute(cubes, cubeNumber + 1, mistakes, 0, 0, 0);
- } else {
- if (i < 5)
- cubes[cubeNumber - 1].rotate(1);
- else if (j < 5) {
- i = 0;
- cubes[cubeNumber - 1].rotate(2);
- j++;
- } else if (k < 5) {
- i = 0;
- j = 0;
- cubes[cubeNumber - 1].rotate(3);
- k++;
- } else {
- if (mistakes < 4) {
- mistakes++;
- cubes[0].rotate(2);
- } else {
- mistakes = 0;
- cubes[0].rotate(3);
- }
- permute(cubes, 2, mistakes, 0, 0, 0);
- stop = true;
- }
- if (!stop) {
- i++;
- permute(cubes, cubeNumber, mistakes, i, j, k);
- }
- }
- }
- string getSolution(vector<Cube> &cubes) {
- int cubeNum = 1;
- string res = "";
- for (Cube &cube : cubes) {
- res += "\nРешение для кубика №" + to_string(cubeNum) + '\n';
- res += "top = " + cube.top + '\n';
- res += "bottom = " + cube.bottom + '\n';
- res += "left = " + cube.left + '\n';
- res += "right = " + cube.right + '\n';
- res += "front = " + cube.front + '\n';
- res += "back = " + cube.back + '\n';
- cubeNum++;
- }
- return res;
- }
- bool isUserWantToSave() {
- bool notCorrect = true;
- string choice;
- cout << "Хотите сохранить результат в файл?" << endl;
- while(notCorrect) {
- cout << "Введите либо [д], либо [н]" << endl;
- getline(cin, choice);
- if(choice == "д" || choice == "н")
- notCorrect = false;
- }
- return choice == "д";
- }
- void saveResult(string res) {
- if(isUserWantToSave()) {
- cout << "Введите полное имя файла" << endl;
- string filePath;
- getline(cin, filePath);
- ofstream outputFile(filePath);
- if(outputFile.is_open()) {
- outputFile << res;
- cout << "Результат успешно сохранен в файл." << endl;
- } else
- cout << "Произошла ошибка при попытке сохранить данные в файл." << endl;
- }
- }
- int main() {
- cout
- << "Данная программа решает головоломку с кубиками, меняя стороны кубиков таким образом, чтобы они образовывали прямоугольную призму, каждая боковая грань которой раскрашена во все четыре цвета."
- << endl;
- Cube cube1 = createCube("голубой", "красный", "зеленый", "зеленый", "белый", "голубой");
- Cube cube2 = createCube("зеленый", "красный", "красный", "красный", "белый", "голубой");
- Cube cube3 = createCube("зеленый", "красный", "белый", "голубой", "белый", "зеленый");
- Cube cube4 = createCube("белый", "зеленый", "красный", "голубой", "белый", "голубой");
- vector<Cube> cubes = {cube1, cube2, cube3, cube4};
- permute(cubes, 2, 1, 0, 0, 0);
- string solution = getSolution(cubes);
- cout << solution << endl;
- saveResult(solution);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement