forked from strang3-r/Leetcode75
-
Notifications
You must be signed in to change notification settings - Fork 0
/
773. Sliding_Puzzle
55 lines (50 loc) · 1.31 KB
/
773. Sliding_Puzzle
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
class Solution {
public:
void swap(char&a,char&b)
{
char c=a;
a=b;
b=c;
}
int slidingPuzzle(vector<vector<int>>& board) {
string expected="123450";
string cur="";
vector<vector<int>> swaps={{1,3},{0,2,4},{1,5},{0,4},{1,3,5},{2,4}};
int z;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++){
cur+=board[i][j]+'0';
if(board[i][j]==0)
z=(i*3)+j;
}
queue<pair<string,int>> q;
unordered_map<string,bool> visited;
visited[cur]=true;
q.push({cur,z});
int level=0;
while(!q.empty())
{
int n=q.size();
for(int i=0;i<n;i++)
{
auto p=q.front();
q.pop();
string st=p.first;
int sw=p.second;
if(st==expected)
return level;
for(auto it:swaps[sw])
{
string s=st;
swap(s[it],s[sw]);
if(!visited[s]){
q.push({s,it});
visited[s]=true;
}
}
}
level++;
}
return -1;
}
};