-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolver.java
More file actions
101 lines (88 loc) · 2.28 KB
/
Solver.java
File metadata and controls
101 lines (88 loc) · 2.28 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.*;
public class Solver {
static final String moves[] = new String[] { "R", "R'", "U", "U'", "F", "F'" };
static byte[] convertScrambleToStickerArray(String scramble) {
byte[] cube = new byte[25];
for (int i = 1; i < cube.length; i++) {
switch (scramble.charAt(i - 1)) {
case 'y':
cube[i] = 0;
break;
case 'r':
cube[i] = 1;
break;
case 'g':
cube[i] = 2;
break;
case 'b':
cube[i] = 3;
break;
case 'w':
cube[i] = 4;
break;
case 'o':
cube[i] = 5;
break;
}
}
return cube;
}
static boolean checkSolved(byte[] cube) {
int color = 0;
for (int i = 1; i < cube.length; i++) {
if (i % 4 == 1) {
color = cube[i];
}
if (cube[i] != color) {
return false;
}
}
return true;
}
static List<String> traceSteps(CubeState cs, Map<CubeState, CubeState> graph) {
List<String> steps = new ArrayList<>();
while (cs != null) {
steps.add(moves[cs.lastMove]);
cs = graph.get(cs);
}
steps.remove(steps.size() - 1);
Collections.reverse(steps);
return steps;
}
static List<String> solveBFS(CubeState cubeState) {
Map<CubeState, CubeState> prev = new HashMap<>();
Queue<CubeState> q = new ArrayDeque<>();
q.add(cubeState);
prev.put(cubeState, null);
while (!q.isEmpty()) {
CubeState curr = q.poll();
if (checkSolved(curr.stickers)) {
return traceSteps(curr, prev);
}
for (CubeState neigh : curr.neighbours()) {
if (!prev.containsKey(neigh)) {
prev.put(neigh, curr);
q.add(neigh);
}
}
}
return null;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 24 character string of sticker colors according to cube_diagram.png\n");
System.out.println("y-yellow");
System.out.println("r-red");
System.out.println("g-green");
System.out.println("b-blue");
System.out.println("w-white");
System.out.println("o-orange");
String scramble = in.next();
byte[] cube = convertScrambleToStickerArray(scramble);
long tic = System.currentTimeMillis();
CubeState init = new CubeState(cube, 0);
List<String> solution = solveBFS(init);
System.out.println("Shortest solution:\n" + solution);
System.out.printf("Time taken to solve: %fs\n", (System.currentTimeMillis() - tic) / 1000f);
}
}