-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
67 lines (56 loc) · 1.36 KB
/
Copy pathsolver.cpp
File metadata and controls
67 lines (56 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include <string>
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <list>
#include "cube.cpp"
#include "solution.cpp"
/**
* The cube solver implements method to solve the rubix cube problem.
*
* Returns a list of cube moves to solve the cube.
*/
class CubeSolver {
public:
/**
* Solve the cube with brute force method.
*
* Attempt all possible move combinations up to certain depth.
*
* If no solution is found for the cube, the code will trow an exception.
*/
static CubeSolution solveBF(Cube cube, int depth=3, CubeSolution solution = CubeSolution()) {
if (cube.solved()) {
solution.solved = true;
return solution;
}
if (depth <= 0) {
return solution;
}
// All possible moves
for (int m = 0; m < 9; m++) {
// CCW / CW
for (int d = 0; d < 2; d++) {
// If its the same move but in oposite direction skip
if (solution.steps.size() > 0) {
CubeStep last = solution.steps.back();
if (last.move == m && last.direction != d) {
continue;
}
}
// Clone solution and add step to list
CubeSolution sol = CubeSolution(solution);
sol.steps.push_back(CubeStep(m, d));
// Clone cube and apply move
Cube c = Cube(&cube);
c.move(m, d);
sol = CubeSolver::solveBF(c, depth - 1, sol);
if (sol.solved) {
return sol;
}
}
}
return solution;
}
};