-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1670.cc
66 lines (54 loc) · 1.11 KB
/
1670.cc
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
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> dist;
int dx[2] = {0, 1};
int dy[2] = {1, 0};
vector<pair<int, int>> edges;
bool IsTerminal(const string& s) {
for (int i = 0; i < 9; i++) {
if ((s[i] - '0') != i) return false;
}
return true;
}
int F(int x, int y) {
return 3 * x + y;
}
int main() {
string s = "";
for (int i = 0; i < 9; i++) {
int x;
cin >> x;
s += (char)('0' + x - 1);
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int dir = 0; dir < 2; dir++) {
int nx = i + dx[dir];
int ny = j + dy[dir];
if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
edges.push_back({F(i, j), F(nx, ny)});
}
}
}
}
queue<string> bfs;
bfs.push(s);
dist[s] = 0;
while(!bfs.empty()) {
auto item = bfs.front();
bfs.pop();
if (IsTerminal(item)) {
cout << dist[item] << "\n";
return 0;
}
int new_dist = dist[item] + 1;
for (auto& x: edges) {
swap(item[x.first], item[x.second]);
auto iter = dist.insert({item, new_dist});
if (iter.second)
bfs.push(item);
swap(item[x.first], item[x.second]);
}
}
return 0;
}