-
Notifications
You must be signed in to change notification settings - Fork 0
/
773. Sliding Puzzle.cpp
95 lines (81 loc) · 2.43 KB
/
773. Sliding Puzzle.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
class Solution {
public:
int slidingPuzzle(vector<vector<int>>& board) {
unordered_set<string>visited;
vector<vector<int>>dst({{1,2,3}, {4,5,0}});
string target = toString(dst);
int level = 0;
queue<vector<vector<int>>>cur, next;
cur.push(board);
while(!cur.empty()) {
auto node = cur.front();
cur.pop();
string s = toString(node);
if (s == target) {
return level;
}
visited.insert(s);
auto nextStates = nextState(node);
for (auto v: nextStates) {
string t = toString(v);
if (!visited.count(t)) {
next.push(v);
}
}
if (cur.empty()) {
++level;
swap(cur, next);
}
}
return -1;
}
vector<vector<vector<int>>> nextState(vector<vector<int>>& board) {
vector<vector<vector<int>>>res;
auto pos = findZero(board);
int r = pos[0];
int c = pos[1];
int left = c - 1;
int right = c + 1;
int up = r - 1;
int down = r + 1;
if (left >= 0) {
swap(board[r][left], board[r][c]);
res.push_back(board);
swap(board[r][left], board[r][c]);
}
if (right < 3) {
swap(board[r][right], board[r][c]);
res.push_back(board);
swap(board[r][right], board[r][c]);
}
if (up >= 0) {
swap(board[up][c], board[r][c]);
res.push_back(board);
swap(board[up][c], board[r][c]);
}
if (down < 2) {
swap(board[down][c], board[r][c]);
res.push_back(board);
swap(board[down][c], board[r][c]);
}
return res;
}
vector<int> findZero(vector<vector<int>>& board) {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
return {i, j};
}
}
}
}
string toString(vector<vector<int>>& v) {
string s;
for (auto x: v) {
for (auto y: x) {
s += to_string(y) + ",";
}
}
return s;
}
};