-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheight_puzzle_BFS.cpp
75 lines (73 loc) · 1.7 KB
/
eight_puzzle_BFS.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
#include<iostream>
#include<queue>
#include<cstring>
#include<set>
typedef struct{
std::string state;
int dis;
}node;
std::string start = "";
std::string goal = "123456780";
std::set<std::string> s;
int dir[4][2] = {
{-1,0},{1,0},{0,1},{0,-1}
};
int bfs(){
node st;
st.state = start;
st.dis = 0;
if(goal==st.state){
return st.dis;
}
s.insert(st.state);
std::queue<node> q;
q.push(st);
while(!q.empty()){
node now=q.front();
q.pop();
int z;
for(int i = 0;i < 9;i++){
if(now.state[i]=='0'){
z = i;
break;
}
}
int x = z/3;
int y = z%3;
for(int i = 0;i < 4;i++){
int newx = x + dir[i][0];
int newy = y + dir[i][1];
int nz = newx*3+newy;
if(newx>=0&&newx<3&&newy>=0&&newy<3){
node newnode;
newnode.state = now.state;
newnode.dis = now.dis+1;
std::swap(newnode.state[z],newnode.state[nz]);
if(newnode.state==goal){
return newnode.dis;
}
if(!(s.count(newnode.state))){
q.push(newnode);
s.insert(newnode.state);
}
}
}
}
return -1;
}
int main(){
char x;
for(int i = 0;i < 9;i++){
std::cin >> x;
if(x=='x'){
start += "0";
}
else{
start += std::to_string(x-'0');
}
}
//std::cout << start;
int ans = bfs();
std::cout << ans;
return 0;
}