leetcode 773

问题

On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.

A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.

The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].

Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.
Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.
Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
Input: board = [[3,2,4],[1,5,0]]
Output: 14

Note:

  • board will be a 2 x 3 array as described above.
  • board[i][j] will be a permutation of [0, 1, 2, 3, 4, 5].

分析

将board转化为状态然后进行广度优先搜索。

代码1

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
// 20ms
class Solution {
public:
vector<vector<int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};
string getState(vector<vector<int>> &board) {
string ret;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
ret += ('0'+board[i][j]);
}
}
return ret;
}
vector<string> getNextState(string s) {
vector<string> ret;
vector<vector<int>> board(2, vector<int>(3, 0));
int x = 0, y = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = (int)(s[3*i+j]-'0');
if (board[i][j] == 0) {
x = i, y = j;
}
}
}
for (vector<int> &dir: dirs) {
int x_ = x+dir[0];
int y_ = y+dir[1];
if (x_<0 || x_>=2 || y_<0 || y_>=3) continue;
swap(board[x][y], board[x_][y_]);
ret.push_back(getState(board));
swap(board[x][y], board[x_][y_]);
}
return ret;
}
int slidingPuzzle(vector<vector<int>>& board) {
unordered_map<string, int> mp; // state -> level
queue<string> q;

string s = getState(board);
q.push(s);
mp[s] = 0;
while (q.size()) {
string t = q.front();
q.pop();

if (t == "123450")
return mp[t];
int level = mp[t];
for (string &t_: getNextState(t)) {
if (mp.find(t_) != mp.end()) continue;
mp[t_] = level+1;
q.push(t_);
}
}
return -1;
}
};

代码2

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
// 4ms
class Solution {
public:
vector<vector<int>> dirs = {{1,3},
{0,2,4},
{1,5},
{0,4},
{1,3,5},
{2,4}};
int slidingPuzzle(vector<vector<int>>& board) {
unordered_map<string, int> mp; // state -> level
queue<string> q;
string s;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j)
s += ('0'+board[i][j]);
q.push(s);
mp[s] = 0;
while (q.size()) {
string t = q.front();
q.pop();
if (t == "123450")
return mp[t];
int level = mp[t];
int i = t.find('0');
for (int j: dirs[i]) {
swap(t[i], t[j]);
if (mp.find(t) == mp.end()) {
mp[t] = level+1;
q.push(t);
}
swap(t[i], t[j]);
}
}
return -1;
}
};