-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearch.java
368 lines (336 loc) · 11.8 KB
/
Search.java
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package cs.sq12phase;
public class Search {
public static final int INVERSE_SOLUTION = 0x2;
static final int FACE_TURN_METRIC = 0;
static final int WCA_TURN_METRIC = 1;
static final int METRIC = WCA_TURN_METRIC; // only available for optimal solver
private static final int PRUN_INC = METRIC == WCA_TURN_METRIC ? 2 : 1;
int[] move = new int[100];
FullCube c = null;
FullCube d = new FullCube("");
Square sq = new Square();
int length1;
int movelen1;
int maxlen2;
int verbose;
String sol_string;
static int getNParity(int idx, int n) {
int p = 0;
for (int i = n - 2; i >= 0; i--) {
p ^= idx % (n - i);
idx /= (n - i);
}
return p & 1;
}
static {
Shape.init();
Square.init();
}
public String solution(FullCube c, int verbose) {
this.c = c;
this.verbose = verbose;
sol_string = null;
int shape = c.getShapeIdx();
for (length1 = Shape.ShapePrun[shape]; length1 < 100; length1++) {
maxlen2 = Math.min(31 - length1, 17);
if (idaPhase1(shape, Shape.ShapePrun[shape], length1, 0, -1)) {
break;
}
}
return sol_string;
}
public String solution(FullCube c) {
return solution(c, 0);
}
public String solutionOpt(FullCube c, int maxl, int verbose) {
this.c = c;
this.verbose = verbose;
sol_string = null;
int shape = c.getShapeIdx();
for (length1 = Shape.ShapePrunOpt[shape] * PRUN_INC; length1 <= maxl * PRUN_INC; length1 += PRUN_INC) {
if (phase1Opt(shape, Shape.ShapePrunOpt[shape], length1, 0, -1, 0)) {
break;
}
}
return sol_string;
}
public String solutionOpt(FullCube c, int maxl) {
return solutionOpt(c, maxl, 0);
}
static int count0xf(int val) {
val &= val >> 1;
val &= val >> 2;
return Integer.bitCount(val & 0x11111111);
}
boolean phase1Opt(int shape, int prunvalue, int maxl, int depth, int lm, int lastTurns) {
int i = count0xf((lastTurns ^ ~0x000000) & 0xff00ff)
- count0xf((lastTurns ^ ~0x666666) & 0xff00ff);
if (i < 0 || i == 0 && (lastTurns >> 20 & 0xf) >= 6) {
return false;
}
if (maxl / PRUN_INC == 0) {
movelen1 = depth;
if (isSolvedInPhase1()) {
return true;
}
if (maxl == 0) {
return false;
}
}
//try each possible move. First twist;
if (lm != 0) {
int shapex = Shape.TwistMove[shape];
int prun = Shape.ShapePrunOpt[shapex];
if (prun < maxl / PRUN_INC) {
move[depth] = 0;
int next_maxl = (maxl / PRUN_INC - 1) * PRUN_INC;
if (phase1Opt(shapex, prun, next_maxl, depth + 1, 0, lastTurns << 8)) {
return true;
}
}
}
//Try top layer
int shapex = shape;
if (lm <= 0) {
int m = 0;
while (true) {
m += Shape.TopMove[shapex];
shapex = m >> 4;
m &= 0xf;
if (m >= 12) {
break;
}
int prun = Shape.ShapePrunOpt[shapex];
if (prun * PRUN_INC > (maxl + PRUN_INC - 1)) {
break;
} else if (prun * PRUN_INC < (maxl + PRUN_INC - 1)) {
move[depth] = m;
if (phase1Opt(shapex, prun, maxl - 1, depth + 1, 1, lastTurns | m << 4)) {
return true;
}
}
}
}
shapex = shape;
//Try bottom layer
if (lm <= 1) {
int m = 0;
while (true) {
m += Shape.BottomMove[shapex];
shapex = m >> 4;
m &= 0xf;
if (m >= 12) {
break;
}
int prun = Shape.ShapePrunOpt[shapex];
if (prun * PRUN_INC > (maxl + PRUN_INC - 1)) {
break;
} else if (prun * PRUN_INC < (maxl + PRUN_INC - 1)) {
move[depth] = -m;
if (phase1Opt(shapex, prun, maxl - 1, depth + 1, 2, lastTurns | m)) {
return true;
}
}
}
}
return false;
}
boolean idaPhase1(int shape, int prunvalue, int maxl, int depth, int lm) {
if (prunvalue == 0 && maxl < 4) {
movelen1 = depth;
return maxl == 0 && initPhase2();
}
//try each possible move. First twist;
if (lm != 0) {
int shapex = Shape.TwistMove[shape];
int prun = Shape.ShapePrun[shapex];
if (prun < maxl) {
move[depth] = 0;
if (idaPhase1(shapex, prun, maxl - 1, depth + 1, 0)) {
return true;
}
}
}
//Try top layer
if (lm <= 0) {
int m = 0;
int shapex = shape;
while (true) {
m += Shape.TopMove[shapex];
shapex = m >> 4;
m &= 0xf;
if (m >= 12) {
break;
}
int prun = Shape.ShapePrun[shapex];
if (prun > maxl) {
break;
} else if (prun < maxl) {
move[depth] = m;
if (idaPhase1(shapex, prun, maxl - 1, depth + 1, 1)) {
return true;
}
}
}
}
//Try bottom layer
if (lm <= 1) {
int m = 0;
int shapex = shape;
while (true) {
m += Shape.BottomMove[shapex];
shapex = m >> 4;
m &= 0xf;
if (m >= 6) {
break;
}
int prun = Shape.ShapePrun[shapex];
if (prun > maxl) {
break;
} else if (prun < maxl) {
move[depth] = -m;
if (idaPhase1(shapex, prun, maxl - 1, depth + 1, 2)) {
return true;
}
}
}
}
return false;
}
boolean isSolvedInPhase1() {
d.copy(c);
for (int i = 0; i < movelen1; i++) {
d.doMove(move[i]);
}
boolean isSolved = d.isSolved();
if (isSolved) {
sol_string = move2string(movelen1);
}
return isSolved;
}
boolean initPhase2() {
d.copy(c);
for (int i = 0; i < movelen1; i++) {
d.doMove(move[i]);
}
assert Shape.ShapePrun[d.getShapeIdx()] == 0;
d.getSquare(sq);
int edge = sq.edgeperm;
int corner = sq.cornperm;
int ml = sq.ml;
int prun = Math.max(Square.SquarePrun[sq.edgeperm << 1 | ml],
Square.SquarePrun[sq.cornperm << 1 | ml]);
for (int i = prun; i < maxlen2; i++) {
if (idaPhase2(edge, corner, sq.topEdgeFirst, sq.botEdgeFirst, ml, i, movelen1, 0)) {
sol_string = move2string(i + movelen1);
return true;
}
}
return false;
}
String move2string(int len) {
StringBuffer s = new StringBuffer();
int[] outputMoves = new int[len];
if ((verbose & INVERSE_SOLUTION) != 0) {
for (int i = len - 1; i >= 0; i--) {
outputMoves[len - 1 - i] = move[i] > 0 ? (12 - move[i]) : move[i] < 0 ? (-12 - move[i]) : move[i];
}
} else {
for (int i = 0; i < len; i++) {
outputMoves[i] = move[i];
}
}
int top = 0, bottom = 0;
for (int i = 0; i < len; i++) {
int val = outputMoves[i];
if (val > 0) {
top = (val > 6) ? (val - 12) : val;
} else if (val < 0) {
bottom = (-val > 6) ? (-val - 12) : -val;
} else {
if (top == 0 && bottom == 0) {
s.append(" / ");
} else {
s.append('(').append(top).append(",").append(bottom).append(") / ");
}
top = 0;
bottom = 0;
}
}
if (top != 0 || bottom != 0) {
s.append('(').append(top).append(",").append(bottom).append(")");
}
return s.toString();
}
boolean idaPhase2(int edge, int corner, boolean topEdgeFirst, boolean botEdgeFirst, int ml, int maxl, int depth, int lm) {
if (maxl == 0 && !topEdgeFirst && botEdgeFirst) {
assert edge == 0 && corner == 0 && ml == 0;
return true;
}
//try each possible move. First twist;
if (lm != 0 && topEdgeFirst == botEdgeFirst) {
int edgex = Square.TwistMove[edge];
int cornerx = Square.TwistMove[corner];
if (Square.SquarePrun[edgex << 1 | (1 - ml)] < maxl && Square.SquarePrun[cornerx << 1 | (1 - ml)] < maxl) {
move[depth] = 0;
if (idaPhase2(edgex, cornerx, topEdgeFirst, botEdgeFirst, 1 - ml, maxl - 1, depth + 1, 0)) {
return true;
}
}
}
//Try top layer
if (lm <= 0) {
boolean topEdgeFirstx = !topEdgeFirst;
int edgex = topEdgeFirstx ? Square.TopMove[edge] : edge;
int cornerx = topEdgeFirstx ? corner : Square.TopMove[corner];
int m = topEdgeFirstx ? 1 : 2;
int prun1 = Square.SquarePrun[edgex << 1 | ml];
int prun2 = Square.SquarePrun[cornerx << 1 | ml];
while (m < 12 && prun1 <= maxl && prun1 <= maxl) {
if (prun1 < maxl && prun2 < maxl) {
move[depth] = m;
if (idaPhase2(edgex, cornerx, topEdgeFirstx, botEdgeFirst, ml, maxl - 1, depth + 1, 1)) {
return true;
}
}
topEdgeFirstx = !topEdgeFirstx;
if (topEdgeFirstx) {
edgex = Square.TopMove[edgex];
prun1 = Square.SquarePrun[edgex << 1 | ml];
m += 1;
} else {
cornerx = Square.TopMove[cornerx];
prun2 = Square.SquarePrun[cornerx << 1 | ml];
m += 2;
}
}
}
if (lm <= 1) {
boolean botEdgeFirstx = !botEdgeFirst;
int edgex = botEdgeFirstx ? Square.BottomMove[edge] : edge;
int cornerx = botEdgeFirstx ? corner : Square.BottomMove[corner];
int m = botEdgeFirstx ? 1 : 2;
int prun1 = Square.SquarePrun[edgex << 1 | ml];
int prun2 = Square.SquarePrun[cornerx << 1 | ml];
while (m < (maxl > 6 ? 6 : 12) && prun1 <= maxl && prun1 <= maxl) {
if (prun1 < maxl && prun2 < maxl) {
move[depth] = -m;
if (idaPhase2(edgex, cornerx, topEdgeFirst, botEdgeFirstx, ml, maxl - 1, depth + 1, 2)) {
return true;
}
}
botEdgeFirstx = !botEdgeFirstx;
if (botEdgeFirstx) {
edgex = Square.BottomMove[edgex];
prun1 = Square.SquarePrun[edgex << 1 | ml];
m += 1;
} else {
cornerx = Square.BottomMove[cornerx];
prun2 = Square.SquarePrun[cornerx << 1 | ml];
m += 2;
}
}
}
return false;
}
}