-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1670.cpp
More file actions
67 lines (60 loc) · 1.37 KB
/
cses_1670.cpp
File metadata and controls
67 lines (60 loc) · 1.37 KB
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
#include <bits/extc++.h>
using namespace std;
int table[10], tar;
int p[12][2]= {0, 1, 0, 3, 1, 2, 1, 4, 2, 5, 3, 4, 3, 6, 4, 5, 4, 7, 5, 8, 6, 7, 7, 8};
void init()
{
table[0] = 1;
for (int i = 1; i < 10; i++)
table[i] = table[i - 1] * 9;
for (int i = 0; i < 9; i++)
tar += i * table[i];
}
int h(vector<int> &ref)
{
int sum = 0;
for (int i = 0; i < 9; i++)
sum += (ref[i] - 1) * table[i];
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init();
vector<int> field(9);
unordered_map<int, bool> visited;
queue<pair<int, vector<int>>> q;
for (int i = 0; i < 9; i++)
cin >> field[i];
if (h(field) == tar)
{
cout << "0\n";
return 0;
}
visited[h(field)] = true;
q.emplace(0, field);
while (!q.empty())
{
auto [prev ,cur] = q.front();
q.pop();
auto next = cur;
for (int d = 0; d < 12; d++)
{
swap(next[p[d][0]], next[p[d][1]]);
int val = h(next);
if (val == tar)
{
cout << prev + 1 << '\n';
return 0;
}
if (!visited[val])
{
visited[val] = true;
q.emplace(prev + 1, next);
}
swap(next[p[d][0]], next[p[d][1]]);
}
}
return 0;
}