773. Sliding Puzzle

problem

solution

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:
string encode(vector<vector<int>> & board){
string str;
int n = board.size(), m=board[0].size();
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
str+= to_string(board[i][j]);
}
}
return str;
}
int slidingPuzzle(vector<vector<int>>& board) {
// bfs
// [[1,2,3],[4,0,5]]
// encode = "123405"

string target = "123450";
// 紀錄鄰居的index
vector<vector<int>> nei{
{1,3},{0,2,4},{1,5},
{0,4},{1,3,5},{2,4}
};
string str = encode(board);
queue<string> q({str});
unordered_set<string> visited({str});
int step = 0;

while(!q.empty()){
int size = q.size();
for(int i=0;i<size;++i){
string p = q.front();
q.pop();
if(p==target) return step;
// find index of zero
int idx = 0;
for(;p[idx]!='0';idx++);
// 零可以跟他的鄰居做交換
for(int j:nei[idx]){
// 備份一份 p
string temp = p;
swap(temp[idx], temp[j]);
if(visited.find(temp) == visited.end()){
q.push(temp);
visited.insert(temp);
}
}

}
step++;
}

return -1;
}
};