773 Sliding Puzzle

1. Question

On a 2x3board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.
A move consists of choosing0 and a 4-directionally adjacent number and swapping it.
The state of the board is_solved_if and only if theboardis[[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
Input: board = [[1,2,3],[4,0,5]]
2
3
Output: 1
4
5
Explanation: Swap the 0 and the 5 in one move.
Copied!
1
Input: board = [[1,2,3],[5,4,0]]
2
3
Output: -1
4
5
Explanation: No number of moves will make the board solved.
Copied!
1
Input: board = [[4,1,2],[5,0,3]]
2
3
Output: 5
4
5
Explanation: 5 is the smallest number of moves that solves the board.
6
7
An example path:
8
After move 0: [[4,1,2],[5,0,3]]
9
After move 1: [[4,1,2],[0,5,3]]
10
After move 2: [[0,1,2],[4,5,3]]
11
After move 3: [[1,0,2],[4,5,3]]
12
After move 4: [[1,2,0],[4,5,3]]
13
After move 5: [[1,2,3],[4,5,0]]
Copied!
1
Input: board = [[3,2,4],[1,5,0]]
2
3
Output: 14
Copied!
Note:
  • boardwill be a 2 x 3 array as described above.
  • board[i][j]will be a permutation of[0, 1, 2, 3, 4, 5].

2. Implementation

(1) BFS
思路: 通过观察我们知道,每次搜索从0开始,0的移动方向有上下左右四种情况。因为我们以String的形式存储每一步的状态,比如board = [[1,2,3],[4,0,5]], 初始状态表示为 "123405", 可以得到0移动的方向有-1(左), 1(右),-3(上),3(下)。但需要注意的是,在String里index 2和 index 3是相邻的,但在board里却不是,在判断位置是否valid时需要考虑这种特殊情况,具体可以看valid()函数的条件。这题的难点主要是通过观察发现状态转移的所有可能情况,剩下的都是很标准的BFS解法
1
class Solution {
2
public int slidingPuzzle(int[][] board) {
3
if (board == null || board.length == 0) {
4
return -1;
5
}
6
7
String target = "123450";
8
StringBuilder sb = new StringBuilder();
9
10
for (int[] row : board) {
11
for (int num : row) {
12
sb.append(num);
13
}
14
}
15
String start = sb.toString();
16
17
Set<String> visited = new HashSet<>();
18
Queue<String> queue = new LinkedList<>();
19
20
visited.add(start);
21
queue.add(start);
22
23
int[] directions = {-1, 1, -3, 3};
24
int level = 0;
25
26
while (!queue.isEmpty()) {
27
int size = queue.size();
28
29
for (int i = 0; i < size; i++) {
30
String curState = queue.remove();
31
32
if (curState.equals(target)) {
33
return level;
34
}
35
36
int startIndex = curState.indexOf('0');
37
38
for (int k = 0; k < 4; k++) {
39
int nextIndex = startIndex + directions[k];
40
41
if (isValid(startIndex, nextIndex)) {
42
char[] letters = curState.toCharArray();
43
swap(letters, startIndex, nextIndex);
44
String nextState = String.valueOf(letters);
45
46
if (!visited.contains(nextState)) {
47
queue.add(nextState);
48
visited.add(nextState);
49
}
50
}
51
}
52
}
53
++level;
54
}
55
return -1;
56
}
57
58
public boolean isValid(int index1, int index2) {
59
if (index2 < 0 || index2 > 5 || index1 == 2 && index2 == 3 || index1 == 3 && index2 == 2) {
60
return false;
61
}
62
else {
63
return true;
64
}
65
}
66
67
public void swap(char[] letters, int i, int j) {
68
char temp = letters[i];
69
letters[i] = letters[j];
70
letters[j] = temp;
71
}
72
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度((mn)!),m是board的行数,n是board的列数, 最多只有(mn)!的状态,空间复杂度O((mn)!)