forked from fogleman/Ricochet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathricochet.c
353 lines (324 loc) · 9.18 KB
/
ricochet.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DEPTH 32
#define NORTH 0x01
#define EAST 0x02
#define SOUTH 0x04
#define WEST 0x08
#define ROBOT 0x10
#define HAS_WALL(x, wall) (x & wall)
#define HAS_ROBOT(x) (x & ROBOT)
#define SET_ROBOT(x) (x |= ROBOT)
#define UNSET_ROBOT(x) (x &= ~ROBOT)
#define PACK_MOVE(robot, direction) (robot << 4 | direction)
#define PACK_UNDO(robot, start, last) (robot << 16 | start << 8 | last)
#define UNPACK_ROBOT(undo) ((undo >> 16) & 0xff)
#define UNPACK_START(undo) ((undo >> 8) & 0xff)
#define UNPACK_LAST(undo) (undo & 0xff)
#define MAKE_KEY(x) (x[0] | (x[1] << 8) | (x[2] << 16) | (x[3] << 24))
#define bool unsigned int
#define true 1
#define false 0
const unsigned int REVERSE[] = {
0, SOUTH, WEST, 0, NORTH, 0, 0, 0, EAST
};
const int OFFSET[] = {
0, -16, 1, 0, 16, 0, 0, 0, -1
};
typedef struct {
unsigned int grid[256];
unsigned int moves[256];
unsigned int robots[4];
unsigned int token;
unsigned int last;
} Game;
typedef struct {
unsigned int key;
unsigned int depth;
} Entry;
typedef struct {
unsigned int mask;
unsigned int size;
Entry *data;
} Set;
inline void swap(unsigned int *array, unsigned int a, unsigned int b) {
unsigned int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
inline unsigned int make_key(Game *game) {
unsigned int robots[4];
memcpy(robots, game->robots, sizeof(unsigned int) * 4);
if (robots[1] > robots[2]) {
swap(robots, 1, 2);
}
if (robots[2] > robots[3]) {
swap(robots, 2, 3);
}
if (robots[1] > robots[2]) {
swap(robots, 1, 2);
}
return MAKE_KEY(robots);
}
unsigned int hash(unsigned int key) {
key = ~key + (key << 15);
key = key ^ (key >> 12);
key = key + (key << 2);
key = key ^ (key >> 4);
key = key * 2057;
key = key ^ (key >> 16);
return key;
}
void set_alloc(Set *set, unsigned int count) {
for (unsigned int i = 0; i < count; i++) {
set->mask = 0xfff;
set->size = 0;
set->data = (Entry *)calloc(set->mask + 1, sizeof(Entry));
set++;
}
}
void set_free(Set *set, unsigned int count) {
for (unsigned int i = 0; i < count; i++) {
free(set->data);
set++;
}
}
void set_grow(Set *set);
bool set_add(Set *set, unsigned int key, unsigned int depth) {
unsigned int index = hash(key) & set->mask;
Entry *entry = set->data + index;
while (entry->key && entry->key != key) {
index = (index + 1) & set->mask;
entry = set->data + index;
}
if (entry->key) {
if (entry->depth < depth) {
entry->depth = depth;
return true;
}
return false;
}
else {
entry->key = key;
entry->depth = depth;
set->size++;
if (set->size * 2 > set->mask) {
set_grow(set);
}
return true;
}
}
void set_grow(Set *set) {
Set new_set;
new_set.mask = (set->mask << 2) | 3;
new_set.size = 0;
new_set.data = (Entry *)calloc(new_set.mask + 1, sizeof(Entry));
for (unsigned int index = 0; index <= set->mask; index++) {
Entry *entry = set->data + index;
if (entry->key) {
set_add(&new_set, entry->key, entry->depth);
}
}
free(set->data);
set->mask = new_set.mask;
set->size = new_set.size;
set->data = new_set.data;
}
inline bool game_over(Game *game) {
if (game->robots[0] == game->token) {
return true;
}
else {
return false;
}
}
bool can_move(
Game *game,
unsigned int robot,
unsigned int direction)
{
unsigned int index = game->robots[robot];
if (HAS_WALL(game->grid[index], direction)) {
return false;
}
if (game->last == PACK_MOVE(robot, REVERSE[direction])) {
return false;
}
unsigned int new_index = index + OFFSET[direction];
if (HAS_ROBOT(game->grid[new_index])) {
return false;
}
return true;
}
unsigned int compute_move(
Game *game,
unsigned int robot,
unsigned int direction)
{
unsigned int index = game->robots[robot] + OFFSET[direction];
while (true) {
if (HAS_WALL(game->grid[index], direction)) {
break;
}
unsigned int new_index = index + OFFSET[direction];
if (HAS_ROBOT(game->grid[new_index])) {
break;
}
index = new_index;
}
return index;
}
unsigned int do_move(
Game *game,
unsigned int robot,
unsigned int direction)
{
unsigned int start = game->robots[robot];
unsigned int end = compute_move(game, robot, direction);
unsigned int last = game->last;
game->robots[robot] = end;
game->last = PACK_MOVE(robot, direction);
UNSET_ROBOT(game->grid[start]);
SET_ROBOT(game->grid[end]);
return PACK_UNDO(robot, start, last);
}
void undo_move(
Game *game,
unsigned int undo)
{
unsigned int robot = UNPACK_ROBOT(undo);
unsigned int start = UNPACK_START(undo);
unsigned int last = UNPACK_LAST(undo);
unsigned int end = game->robots[robot];
game->robots[robot] = start;
game->last = last;
SET_ROBOT(game->grid[start]);
UNSET_ROBOT(game->grid[end]);
}
void precompute_minimum_moves(
Game *game)
{
bool status[256];
for (unsigned int i = 0; i < 256; i++) {
game->moves[i] = 0xffffffff;
status[i] = false;
}
game->moves[game->token] = 0;
status[game->token] = true;
bool done = false;
while (!done) {
done = true;
for (unsigned int i = 0; i < 256; i++) {
if (!status[i]) {
continue;
}
status[i] = false;
unsigned int depth = game->moves[i] + 1;
for (unsigned int direction = 1; direction <= 8; direction <<= 1) {
unsigned int index = i;
while (!HAS_WALL(game->grid[index], direction)) {
index += OFFSET[direction];
if (game->moves[index] > depth) {
game->moves[index] = depth;
status[index] = true;
done = false;
}
}
}
}
}
}
unsigned int _nodes;
unsigned int _hits;
unsigned int _inner;
unsigned int _search(
Game *game,
unsigned int depth,
unsigned int max_depth,
unsigned char *path,
Set *set)
{
_nodes++;
if (game_over(game)) {
return depth;
}
if (depth == max_depth) {
return 0;
}
unsigned int height = max_depth - depth;
if (game->moves[game->robots[0]] > height) {
return 0;
}
if (height != 1 && !set_add(set, make_key(game), height)) {
_hits++;
return 0;
}
_inner++;
for (unsigned int robot = 0; robot < 4; robot++) {
if (robot && game->moves[game->robots[0]] == height) {
continue;
}
for (unsigned int direction = 1; direction <= 8; direction <<= 1) {
if (!can_move(game, robot, direction)) {
continue;
}
unsigned int undo = do_move(game, robot, direction);
unsigned int result = _search(
game, depth + 1, max_depth, path, set
);
undo_move(game, undo);
if (result) {
path[depth] = PACK_MOVE(robot, direction);
return result;
}
}
}
return 0;
}
unsigned int search(
Game *game,
unsigned char *path,
void (*callback)(unsigned int, unsigned int, unsigned int, unsigned int))
{
if (game_over(game)) {
return 0;
}
unsigned int result = 0;
Set set;
set_alloc(&set, 1);
precompute_minimum_moves(game);
for (unsigned int max_depth = 1; max_depth < MAX_DEPTH; max_depth++) {
_nodes = 0;
_hits = 0;
_inner = 0;
result = _search(game, 0, max_depth, path, &set);
if (callback) {
callback(max_depth, _nodes, _inner, _hits);
}
if (result) {
break;
}
}
set_free(&set, 1);
return result;
}
void _callback(
unsigned int depth,
unsigned int nodes,
unsigned int inner,
unsigned int hits)
{
printf("%u %u %u %u\n", depth, nodes, inner, hits);
}
int main(int argc, char *argv[]) {
Game game = {
{9, 1, 5, 1, 3, 9, 1, 1, 1, 3, 9, 1, 1, 1, 1, 3, 8, 2, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 3, 8, 0, 0, 0, 0, 2, 12, 0, 2, 9, 0, 0, 0, 0, 4, 2, 12, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 10, 9, 0, 0, 0, 3, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 8, 6, 8, 0, 0, 0, 0, 4, 4, 0, 0, 2, 12, 0, 0, 2, 8, 1, 0, 0, 0, 0, 2, 9, 3, 8, 0, 0, 1, 0, 0, 2, 8, 0, 4, 0, 2, 12, 2, 12, 6, 8, 0, 0, 0, 0, 0, 6, 8, 18, 9, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 4, 0, 3, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 9, 0, 2, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 0, 0, 2, 9, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 0, 2, 12, 2, 8, 0, 0, 16, 3, 8, 0, 0, 0, 4, 0, 0, 0, 0, 1, 2, 8, 6, 8, 0, 0, 0, 0, 0, 0, 3, 8, 0, 0, 0, 16, 2, 12, 5, 4, 4, 4, 6, 12, 4, 4, 4, 4, 6, 12, 4, 4, 6},
{0},
{176, 145, 211, 238},
54,
0
};
unsigned char path[32];
search(&game, path, _callback);
}