-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShape.java
198 lines (178 loc) · 6.18 KB
/
Shape.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
package cs.sq12phase;
import java.util.Arrays;
class Shape {
//1 = corner, 0 = edge.
static int[] halflayer = {0x00, 0x03, 0x06, 0x0c, 0x0f, 0x18, 0x1b, 0x1e,
0x30, 0x33, 0x36, 0x3c, 0x3f
};
static int[] ShapeIdx = new int[3678];
static int[] ShapePrun = new int[3678 * 2];
static int[] ShapePrunOpt = new int[3678 * 2];
static int[] TopMove = new int[3678 * 2];
static int[] BottomMove = new int[3678 * 2];
static int[] TwistMove = new int[3678 * 2];
private Shape() {}
int top;
int bottom;
int parity;
static int getShape2Idx(int shp) {
int ret = Arrays.binarySearch(ShapeIdx, shp & 0xffffff) << 1 | shp >> 24;
return ret;
}
int getIdx() {
int ret = Arrays.binarySearch(ShapeIdx, top << 12 | bottom) << 1 | parity;
return ret;
}
void setIdx(int idx) {
parity = idx & 1;
top = ShapeIdx[idx >> 1];
bottom = top & 0xfff;
top >>= 12;
}
int topMove() {
int move = 0;
int moveParity = 0;
do {
if ((top & 0x800) == 0) {
move += 1;
top = top << 1;
} else {
move += 2;
top = (top << 2) ^ 0x3003;
}
moveParity = 1 - moveParity;
} while ((Integer.bitCount(top & 0x3f) & 1) != 0);
if ((Integer.bitCount(top) & 2) == 0) {
parity ^= moveParity;
}
return move;
}
int bottomMove() {
int move = 0;
int moveParity = 0;
do {
if ((bottom & 0x800) == 0) {
move += 1;
bottom = bottom << 1;
} else {
move += 2;
bottom = (bottom << 2) ^ 0x3003;
}
moveParity = 1 - moveParity;
} while ((Integer.bitCount(bottom & 0x3f) & 1) != 0);
if ((Integer.bitCount(bottom) & 2) == 0) {
parity ^= moveParity;
}
return move;
}
void twistMove() {
int temp = top & 0x3f;
int p1 = Integer.bitCount(temp);
int p3 = Integer.bitCount(bottom & 0xfc0);
parity ^= 1 & ((p1 & p3) >> 1);
top = (top & 0xfc0) | ((bottom >> 6) & 0x3f);
bottom = (bottom & 0x3f) | temp << 6;
}
static boolean inited = false;
static void initPruning(int[] Prun, int done, int metric) {
int done0 = 0;
int depth = -1;
while (done != done0) {
done0 = done;
++depth;
System.out.println(String.format("%2d%6d", depth, done));
for (int i = 0; i < 3678 * 2; i++) {
if (Prun[i] != depth) {
continue;
}
// try twist
{
int idx = TwistMove[i];
if (Prun[idx] == -1) {
++done;
Prun[idx] = depth + 1;
}
}
if (metric == Search.FACE_TURN_METRIC) {
// try top move
for (int m = 0, inc = 0, idx = i; m != 12; m += inc) {
idx = TopMove[idx];
inc = idx & 0xf;
idx >>= 4;
if (Prun[idx] == -1) {
++done;
Prun[idx] = depth + 1;
}
}
// try bottom
for (int m = 0, inc = 0, idx = i; m != 12; m += inc) {
idx = BottomMove[idx];
inc = idx & 0xf;
idx >>= 4;
if (Prun[idx] == -1) {
++done;
Prun[idx] = depth + 1;
}
}
} else if (metric == Search.WCA_TURN_METRIC) {
// try top/bottom move
for (int m = 0, inc = 0, idx = i; m != 12; m += inc) {
idx = TopMove[idx];
inc = idx & 0xf;
idx >>= 4;
for (int m2 = 0, inc2 = 0, idx2 = idx; m2 != 12; m2 += inc2) {
idx2 = BottomMove[idx2];
inc2 = idx2 & 0xf;
idx2 >>= 4;
if (Prun[idx2] == -1) {
++done;
Prun[idx2] = depth + 1;
}
}
}
}
}
}
}
static void init() {
if (inited) {
return;
}
int count = 0;
for (int i = 0; i < 13 * 13 * 13 * 13; i++) {
int dr = halflayer[i % 13];
int dl = halflayer[i / 13 % 13];
int ur = halflayer[i / 13 / 13 % 13];
int ul = halflayer[i / 13 / 13 / 13];
int value = ul << 18 | ur << 12 | dl << 6 | dr;
if (Integer.bitCount(value) == 16) {
ShapeIdx[count++] = value;
}
}
System.out.println(count);
Shape s = new Shape();
for (int i = 0; i < 3678 * 2; i++) {
s.setIdx(i);
TopMove[i] = s.topMove();
TopMove[i] |= s.getIdx() << 4;
s.setIdx(i);
BottomMove[i] = s.bottomMove();
BottomMove[i] |= s.getIdx() << 4;
s.setIdx(i);
s.twistMove();
TwistMove[i] = s.getIdx();
}
for (int i = 0; i < 3678 * 2; i++) {
ShapePrun[i] = -1;
ShapePrunOpt[i] = -1;
}
ShapePrun[getShape2Idx(0x0db66db)] = 0; //0 110110110110 011011011011
ShapePrun[getShape2Idx(0x1db6db6)] = 0; //1 110110110110 110110110110
ShapePrun[getShape2Idx(0x16db6db)] = 0; //1 011011011011 011011011011
ShapePrun[getShape2Idx(0x06dbdb6)] = 0; //0 011011011011 110110110110
ShapePrunOpt[new FullCube().getShapeIdx()] = 0;
initPruning(ShapePrun, 4, Search.FACE_TURN_METRIC);
initPruning(ShapePrunOpt, 1, Search.METRIC);
inited = true;
}
}