-
Notifications
You must be signed in to change notification settings - Fork 1
/
100000609-03.cpp
93 lines (85 loc) · 2.11 KB
/
100000609-03.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
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
struct Node {
vector<int> mb; // magic board
vector<char> step;
} start;
set<vector<int> > in_queue;
void bfs(vector<int> &target)
{
queue<Node> q;
q.push(start);
while (!q.empty())
{
Node front = q.front();
q.pop();
// 退出条件
if (front.mb == target)
{
printf("%d\n", front.step.size());
for (int i = 0; i < front.step.size(); i++)
printf("%c", front.step[i]);
printf("\n");
return;
}
Node tmp;
// instruction A
tmp = front;
reverse(tmp.mb.begin(), tmp.mb.end());
if (!in_queue.count(tmp.mb))
{
in_queue.insert(tmp.mb);
tmp.step.push_back('A');
q.push(tmp);
}
// instruction B
tmp = front;
tmp.mb.clear();
tmp.mb.push_back(front.mb[3]);
for (int i = 0; i < 3; i++)
tmp.mb.push_back(front.mb[i]);
for (int i = 5; i < 8; i++)
tmp.mb.push_back(front.mb[i]);
tmp.mb.push_back(front.mb[4]);
if (!in_queue.count(tmp.mb))
{
in_queue.insert(tmp.mb);
tmp.step.push_back('B');
q.push(tmp);
}
// instruction C
tmp = front;
tmp.mb[1] = front.mb[6];
tmp.mb[2] = front.mb[1];
tmp.mb[5] = front.mb[2];
tmp.mb[6] = front.mb[5];
if (!in_queue.count(tmp.mb))
{
in_queue.insert(tmp.mb);
tmp.step.push_back('C');
q.push(tmp);
}
}
}
int main()
{
// initialize
for (int i = 0; i < 8; i++)
start.mb.push_back(i + 1);
int tmp[8];
while (scanf("%d%d%d%d%d%d%d%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5], &tmp[6], &tmp[7]) == 8)
{
// initialize
vector<int> target;
for (int i = 0; i < 8; i++)
target.push_back(tmp[i]);
in_queue.clear();
// bfs
bfs(target);
}
return 0;
}