-
Notifications
You must be signed in to change notification settings - Fork 0
/
acmTrain openLock
116 lines (110 loc) · 1.95 KB
/
acmTrain openLock
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
#include <cstdio>
#include <math.h>
#include <queue>
using namespace std;
struct Node
{
char number[4];
int flag;
};
char number1[4];
char number2[4];
char Temp;
int main()
{
int T = 0;
int s = 0;
int sum = 0;
int bfs(Node curr);
Node src;
scanf("%d", &T);
getchar();
while (T--)
{
sum = 0;
for (int i = 0; i<4; i++)
scanf("%c", &number1[i]);
getchar();
for (int i = 0; i<4; i++)
scanf("%c", &number2[i]);
getchar();
src.number[0] = number1[0];
src.number[1] = number1[1];
src.number[2] = number1[2];
src.number[3] = number1[3];
src.flag = 0;
printf("%d\n",bfs(src));
}
return 0;
}
int bfs(Node curr)
{
queue<Node> q;
q.push(curr);
while (!q.empty())
{
Node next;
int i = 0;
while (i < 11)
{
switch (i)
{
case 0:
next.number[0]++;
next.flag=curr.flag+1;
break;
case 1:
next.number[1]++;
next.flag = curr.flag + 1;
break;
case 2:
next.number[2]++;
next.flag = curr.flag + 1;
break;
case 3:
next.number[3]++;
next.flag = curr.flag + 1;
break;
case 4:
next.number[0]--;
next.flag = curr.flag + 1;
break;
case 5:
next.number[1]--;
next.flag = curr.flag + 1;
break;
case 6:
next.number[2]--;
next.flag = curr.flag + 1;
break;
case 7:
next.number[3]--;
next.flag = curr.flag + 1;
break;
case 8:
Temp = next.number[0];
next.number[0] = next.number[1];
next.number[1] = Temp;
next.flag = curr.flag + 1;
break;
case 9:
Temp = next.number[1];
next.number[1] = next.number[2];
next.number[2] = Temp;
next.flag = curr.flag + 1;
break;
case 10:
Temp = next.number[2];
next.number[2] = next.number[3];
next.number[3] = Temp;
next.flag = curr.flag + 1;
break;
}
if (next.number[0] == number2[0] && next.number[1] == number2[1] && next.number[2] == number2[2] && next.number[3] == number2[3])
return next.flag;
else
bfs(next);
i++;
}
}
}