-
Notifications
You must be signed in to change notification settings - Fork 156
/
1670 - Swap Game.cpp
107 lines (103 loc) · 3 KB
/
1670 - Swap Game.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Problem Name: Swap Game
Problem Link: https://cses.fi/problemset/task/1670
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define endl '\n'
unordered_set<int> vis;
int go(int x1,int x2, int x3,int x4,int x5,int x6,int x7,int x8,int x9) {
return x1*100000000 + x2*10000000 + x3*1000000
+ x4*100000 + x5*10000 + x6*1000 + x7*100 + x8*10 + x9;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int x = 0;
for (int i = 0; i < 9; i++) {
int a; cin>>a;
x = x*10+a;
}
queue<pair<int, int>> q;
q.push({123456789, 0});
vis.insert(123456789);
while(!q.empty()) {
auto [s, c] = q.front();
q.pop();
if (s == x) return cout<<c, 0;
int x9 = s%10; s/=10;
int x8 = s%10; s/=10;
int x7 = s%10; s/=10;
int x6 = s%10; s/=10;
int x5 = s%10; s/=10;
int x4 = s%10; s/=10;
int x3 = s%10; s/=10;
int x2 = s%10; s/=10;
int x1 = s%10; s/=10;
int n1 = go(x2, x1, x3, x4, x5, x6, x7, x8, x9);
int n2 = go(x1, x3, x2, x4, x5, x6, x7, x8, x9);
int n3 = go(x1, x2, x3, x5, x4, x6, x7, x8, x9);
int n4 = go(x1, x2, x3, x4, x6, x5, x7, x8, x9);
int n5 = go(x1, x2, x3, x4, x5, x6, x8, x7, x9);
int n6 = go(x1, x2, x3, x4, x5, x6, x7, x9, x8);
int n7 = go(x4, x2, x3, x1, x5, x6, x7, x8, x9);
int n8 = go(x1, x2, x3, x7, x5, x6, x4, x8, x9);
int n9 = go(x1, x5, x3, x4, x2, x6, x7, x8, x9);
int n10 = go(x1, x2, x3, x4, x8, x6, x7, x5, x9);
int n11 = go(x1, x2, x6, x4, x5, x3, x7, x8, x9);
int n12 = go(x1, x2, x3, x4, x5, x9, x7, x8, x6);
if (!vis.count(n1)) {
q.push({n1, c+1});
vis.insert(n1);
}
if (!vis.count(n2)) {
q.push({n2, c+1});
vis.insert(n2);
}
if (!vis.count(n3)) {
q.push({n3, c+1});
vis.insert(n3);
}
if (!vis.count(n4)) {
q.push({n4, c+1});
vis.insert(n4);
}
if (!vis.count(n5)) {
q.push({n5, c+1});
vis.insert(n5);
}
if (!vis.count(n6)) {
q.push({n6, c+1});
vis.insert(n6);
}
if (!vis.count(n7)) {
q.push({n7, c+1});
vis.insert(n7);
}
if (!vis.count(n8)){
q.push({n8, c+1});
vis.insert(n8);
}
if (!vis.count(n9)){
q.push({n9, c+1});
vis.insert(n9);
}
if (!vis.count(n10)){
q.push({n10, c+1});
vis.insert(n10);
}
if (!vis.count(n11)){
q.push({n11, c+1});
vis.insert(n11);
}
if (!vis.count(n12)){
q.push({n12, c+1});
vis.insert(n12) ;
}
}
}