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
| #include <iostream>
using namespace std;
char c;
char str1[10], str2[10];
bool vis[10000];
int ttt(char x) {
if (x == 'A') return 1;
if (x == 'B') return 2;
if (x == 'C') return 3;
return 0;
}
int S, T;
int q[10000];
int conv(int one, int two, int three, int four) {
return one*1000+two*100+three*10+four;
}
void bfs(int S) {
int head = 0;
int tail = 1;
q[0] = S;
vis[S] = true;
int now, tmp, pos;
int one, two, three, four;
while (head < tail) {
now = q[head++];
for (tmp = now, pos = 4; tmp % 10; tmp /= 10, --pos);
tmp = now;
four = tmp % 10; tmp /= 10;
three = tmp % 10; tmp /= 10;
two = tmp % 10; tmp /= 10;
one = tmp;
if (pos == 1) {
tmp = conv(three, two, one, four);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
tmp = conv(two, one, three, four);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
continue;
}
if (pos == 2) {
tmp = conv(two, one, three, four);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
tmp = conv(one, four, three, two);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
continue;
}
if (pos == 3) {
tmp = conv(one, two, four, three);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
tmp = conv(three, two, one, four);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
continue;
}
if (pos == 4) {
tmp = conv(one, four, three, two);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
tmp = conv(one, two, four, three);
if (!vis[tmp]) {
vis[tmp] = true;
q[tail++] = tmp;
}
continue;
}
}
}
int main () {
cin >> c; str1[1] = c; cin >> c; str1[2] = c; cin >> c; str1[3] = c; cin >> c; str1[4] = c;
cin >> c; str2[1] = c; cin >> c; str2[2] = c; cin >> c; str2[3] = c; cin >> c; str2[4] = c;
S = ttt(str1[1])*1000+ttt(str1[2])*100+ttt(str1[3])*10+ttt(str1[4]);
T = ttt(str2[1])*1000+ttt(str2[2])*100+ttt(str2[3])*10+ttt(str2[4]);
bfs(S);
if (vis[T]) cout << "YES";
else cout << "NO";
}
|