-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode941.cpp
80 lines (74 loc) · 2.35 KB
/
lintcode941.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
/**
* 這裏visited裡面保存的是地圖的狀態
* 我看到input size很小時就有在考慮狀態壓縮了
* 只是沒想到他這裡用string的方式來保存地圖狀態
* queue裡面保存的也是地圖的狀態
* 還有當初漏看了題意:move是由0和隔壁的元素交換
* 所以我們只要找出0的位置然後跟隔壁的位置交換即可
* library function這方面都蠻齊全的
**/
class Solution {
public:
/**
* @param board: the given board
* @return: the least number of moves required so that the state of the board is solved
*/
string hash(vector<vector<int>> &board) {
string res = "";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
res += to_string(board[i][j]);
}
}
return res;
}
bool isValid(int x, int y) {
if(x < 0 || y < 0 || x >= 2 || y >= 3)
return false;
return true;
}
vector<string> getNxtStatus(string &cur) {
vector<string> res;
int zero = cur.find("0");
int x = zero / 3;
int y = zero % 3;
vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for(auto dir: dirs) {
int nxtX = x + dir[0];
int nxtY = y + dir[1];
if(!isValid(nxtX, nxtY))
continue;
int newZero = nxtX*3 + nxtY;
swap(cur[zero], cur[newZero]);
res.push_back(cur);
swap(cur[zero], cur[newZero]);
}
return res;
}
int slidingPuzzle(vector<vector<int>> &board) {
string start = hash(board);
string end = "123450";
unordered_set<string> visited;
queue<string> q;
int steps = 0;
q.push(start);
visited.insert(start);
while(!q.empty()) {
int sz = q.size();
for(int tmp = 0; tmp < sz; tmp++) {
string cur = q.front();
q.pop();
if(cur == end)
return steps;
for(auto nxtStat : getNxtStatus(cur)) {
if(!visited.count(nxtStat)) {
visited.insert(nxtStat);
q.push(nxtStat);
}
}
}
steps++;
}
return -1;
}
};