-
Notifications
You must be signed in to change notification settings - Fork 103
/
ANARC08A.cpp
86 lines (77 loc) · 1.59 KB
/
ANARC08A.cpp
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
/*
USER: zobayer
TASK: ANARC08A
ALGO: breadth first search
*/
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
using namespace std;
struct trie {
int idx;
trie *next[9];
trie() { for(int i=0; i<9; i++) next[i]=NULL; idx = -1; }
} *T;
inline int find(char *s) {
trie *curr = T; int i, k;
for(i = 0; s[i]; i++) {
k = s[i] - '1';
if(curr->next[k] == NULL) return -1;
curr = curr->next[k];
}
return curr->idx;
}
inline int check(char *s, int &p) {
trie *curr = T; int i, k;
for(i = 0; s[i]; i++) {
k = s[i] - '1';
if(curr->next[k] == NULL) curr->next[k] = new trie;
curr = curr->next[k];
}
if(curr->idx == -1) curr->idx = p++;
return curr->idx;
}
int moves[8][4] = {
{0,3,4,1}, {1,4,5,2}, {3,6,7,4}, {4,7,8,5},
{0,1,4,3}, {1,2,5,4}, {3,4,7,6}, {4,5,8,7}
};
const int MAX = 360980;
char s[MAX][10] = {"123456789"};
int d[MAX];
void bfs() {
int u, v, i, j, k, p;
char temp[10] = {0}, x;
queue< int > Q;
Q.push(0); d[0] = k = 0;
check(s[0], k);
while(!Q.empty()) {
u = Q.front(); Q.pop();
if(d[u] >= 9) continue;
for(i = 0; i < 8; i++) {
strcpy(temp, s[u]);
for(j = 1, x = temp[moves[i][0]]; j < 4; j++)
temp[moves[i][j-1]] = temp[moves[i][j]];
temp[moves[i][j-1]] = x;
p = k; v = check(temp, k);
if(p != k) {
d[v] = d[u] + 1;
Q.push(v);
strcpy(s[v], temp);
}
}
}
}
int main() {
char inp[12];
int f, k, cs = 1;
T = new trie;
bfs();
while(scanf("%s", inp)==1 && inp[1]>'0') {
k = inp[0] - '0';
f = find(&inp[1]);
if(f == -1 || d[f] > k) printf("%d. -1\n", cs++);
else printf("%d. %d\n", cs++, d[f]);
}
return 0;
}