generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
63 lines (55 loc) · 1.74 KB
/
solution.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
#include <stdint.h>
#include <string.h>
uint16_t to_int(char* str) {
return (str[0] - '0') << 12 | (str[1] - '0') << 8 | (str[2] - '0') << 4 | (str[3] - '0');
}
int openLock(char** deadends, int deadendsSize, char* targetStr) {
if (targetStr[0] == '0' && targetStr[1] == '0' && targetStr[2] == '0' && targetStr[3] == '0') return 0;
uint8_t seen[8192];
memset(seen, 0, 8192 * sizeof(uint8_t));
for (int i = 0; i < deadendsSize; ++i) {
int deadend = to_int(deadends[i]);
seen[deadend / 8] |= 1 << deadend % 8;
}
if ((seen[0] & 1) == 1) return -1;
uint16_t queue[1500];
int queueBegin = 0;
int queueEnd = 1;
queue[queueBegin] = 0;
seen[0] |= 1;
int steps = 1;
uint16_t target = to_int(targetStr);
while (queueBegin != queueEnd) {
for (int i = (queueBegin < queueEnd ? queueEnd : 1500 + queueEnd) - queueBegin; i > 0; --i) {
for (int shift = 0; shift <= 12; shift += 4) {
int key = queue[queueBegin];
if ((key >> shift & 15) == 9) {
key -= 9 << shift;
} else {
key += 1 << shift;
}
if (key == target) return steps;
if ((seen[key / 8] >> key % 8 & 1) == 0) {
queue[queueEnd] = key;
if (++queueEnd == 1500) queueEnd = 0;
seen[key / 8] |= 1 << key % 8;
}
key = queue[queueBegin];
if ((key >> shift & 15) == 0) {
key += 9 << shift;
} else {
key -= 1 << shift;
}
if (key == target) return steps;
if ((seen[key / 8] >> key % 8 & 1) == 0) {
queue[queueEnd] = key;
if (++queueEnd == 1500) queueEnd = 0;
seen[key / 8] |= 1 << key % 8;
}
}
if (++queueBegin == 1500) queueBegin = 0;
}
++steps;
}
return -1;
}