-
Notifications
You must be signed in to change notification settings - Fork 1
/
P10067.cpp
96 lines (92 loc) · 1.97 KB
/
P10067.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
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <set>
#include <queue>
unsigned int read4() {
int m;
unsigned int s = 0;
for(int i = 0; i < 4; ++i) {
std::cin >> m;
s = 10*s+m;
}
return s;
}
int rotUp(int input, int wheel) {
switch(wheel) {
case 0: // singles:
if((input % 10) == 9)
return input-9;
return input+1;
case 1:
if((input/10 % 10) == 9)
return input-90;
return input+10;
case 2:
if((input/100 % 10) == 9)
return input-900;
return input+100;
default: // case 3:
if(input >= 9000)
return input-9000;
return input+1000;
}
}
int rotDown(int input, int wheel) {
switch(wheel) {
case 0: // singles:
if((input % 10) == 0)
return input+9;
return input-1;
case 1:
if((input/10 % 10) == 0)
return input+90;
return input-10;
case 2:
if((input/100 % 10) == 0)
return input+900;
return input-100;
default: // case 3:
if(input < 1000)
return input+9000;
return input-1000;
}
}
int main() {
int cases, m, dist[10000];
std::cin >> cases;
for(int cas = 0; cas < cases; ++cas) {
for(int i = 0; i < 10000; ++i)
dist[i] = -1;
int s = read4();
dist[s] = 0;
int t = read4();
std::set<int> forbidden;
std::cin >> m;
for(int i = 0; i < m; ++i)
forbidden.insert(read4());
std::queue<int> q;
q.push(s);
while(!q.empty()) {
int top = q.front();
if(top == t)
break;
q.pop();
int newDist = dist[top]+1;
// push all related:
for(int i = 0; i < 4; ++i) {
int neighbour = rotUp(top, i);
if(dist[neighbour] != -1 || forbidden.find(neighbour) != forbidden.end())
continue;
dist[neighbour] = newDist;
q.push(neighbour);
}
for(int i = 0; i < 4; ++i) {
int neighbour = rotDown(top, i);
if(dist[neighbour] != -1 || forbidden.find(neighbour) != forbidden.end())
continue;
dist[neighbour] = newDist;
q.push(neighbour);
}
}
std::cout << dist[t] << std::endl;
}
}