yarin0600

Untitled

May 7th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include "cube/include/cube_step.hh"
  2. #include "cube/include/cube.hh"
  3. #include "cube/include/cube_movements.hh"
  4.  
  5. #include <iostream>
  6. #include <queue>
  7. #include <string>
  8. #include <functional>
  9. #include <unordered_set>
  10.  
  11. struct CubeMove
  12. {
  13.   std::string sourceConfiguration;
  14.   cube::Step stepTaken;
  15. };
  16.  
  17. struct MovementFunction
  18. {
  19.   std::function<cube::Cube(cube::Cube const &)> movementFunction;
  20.   cube::Step step;
  21. };
  22.  
  23. std::vector<cube::Step> solveCube(cube::Cube const &sourceCube)
  24. {
  25.   static std::string const solvedEncoded = "RRRRRRRRRGGGGGGGGGOOOOOOOOOBBBBBBBBBWWWWWWWWWYYYYYYYYY";
  26.   static std::vector<MovementFunction> moveAndFuncPairs = {
  27.       {.movementFunction = cube::movements::movedRightLayer, .step = cube::Step::R},
  28.       {.movementFunction = cube::movements::movedRightPrimeLayer, .step = cube::Step::R_},
  29.       {.movementFunction = cube::movements::movedLeftLayer, .step = cube::Step::L},
  30.       {.movementFunction = cube::movements::movedLeftPrimeLayer, .step = cube::Step::L_},
  31.       {.movementFunction = cube::movements::movedLeftLayer, .step = cube::Step::U},
  32.       {.movementFunction = cube::movements::movedLeftPrimeLayer, .step = cube::Step::U_},
  33.       {.movementFunction = cube::movements::movedDownLayer, .step = cube::Step::D},
  34.       {.movementFunction = cube::movements::movedDownPrimeLayer, .step = cube::Step::D_},
  35.       {.movementFunction = cube::movements::movedFrontLayer, .step = cube::Step::F},
  36.       {.movementFunction = cube::movements::movedFrontPrimeLayer, .step = cube::Step::F_},
  37.       {.movementFunction = cube::movements::movedBackLayer, .step = cube::Step::B},
  38.       {.movementFunction = cube::movements::movedBackPrimeLayer, .step = cube::Step::B_}};
  39.   std::unordered_set<std::string> exploredConfigurations;
  40.   std::unordered_map<std::string, CubeMove> encConfToParentCubeMove;
  41.   std::queue<std::string> encodedCubes;
  42.   std::string const sourceCubeEncoded = sourceCube.encode();
  43.   exploredConfigurations.insert(sourceCubeEncoded);
  44.   encodedCubes.push(sourceCubeEncoded);
  45.   while (true)
  46.   {
  47.     std::string currentCubeEncodedConfiguration = encodedCubes.front();
  48.     if (currentCubeEncodedConfiguration == solvedEncoded)
  49.     {
  50.       break;
  51.     }
  52.     cube::Cube currentCubeEncoded = cube::Cube::decode(currentCubeEncodedConfiguration);
  53.     encodedCubes.pop();
  54.     for (auto const &[movementFunction, step] : moveAndFuncPairs)
  55.     {
  56.       cube::Cube const movedCube = movementFunction(currentCubeEncoded);
  57.       std::string movedCubeEncoded = movedCube.encode();
  58.       if (not exploredConfigurations.contains(movedCubeEncoded))
  59.       {
  60.         encodedCubes.push(movedCubeEncoded); // add encoded cube to the queue
  61.         exploredConfigurations.insert(movedCubeEncoded);
  62.         encConfToParentCubeMove[movedCubeEncoded] = CubeMove{
  63.             .sourceConfiguration = currentCubeEncoded,
  64.             .stepTaken = step};
  65.       }
  66.     }
  67.   }
  68.   std::vector<cube::Step> solvingSteps;
  69.   std::string currentEncoded = solvedEncoded;
  70.   while (currentEncoded != sourceCubeEncoded)
  71.   {
  72.     auto const &[parentEncoded, stepTaken] = encConfToParentCubeMove[currentEncoded];
  73.     currentEncoded = parentEncoded;
  74.     solvingSteps.push_back(stepTaken);
  75.   }
  76.   std::reverse(solvingSteps.begin(), solvingSteps.end());
  77.   return solvingSteps;
  78. }
  79.  
  80. int main()
  81. {
  82.  
  83.   // std::string cubeEncoded = "GYRBRORRGYGRYGOORWWGYWOBOYYORROBYOWBBRGGWBWWWBWGBYOYGB";
  84.   std::string cubeEncoded = "RRORRORROYGGYGGYGGROOROOROOWBBWBBWBBYGGYWWBWWBBWYYWYYG";
  85.   cube::Cube cube(cubeEncoded);
  86.   auto steps = solveCube(cube);
  87.   if (steps.empty())
  88.   {
  89.     std::cout << "the cube is already solved\n";
  90.   }
  91.   else
  92.   {
  93.     for (cube::Step const step : steps)
  94.     {
  95.       std::cout << step << std::endl;
  96.     }
  97.   }
  98.   return 0;
  99. }
  100.  
Add Comment
Please, Sign In to add comment