LC.P773[滑动谜题]

方法一:BFS

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 {
int[][] neighbors = {{1, 3}, {0, 2, 4}, {1, 5}, {0, 4}, {1, 3, 5}, {2, 4}};

public int slidingPuzzle(int[][] board) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
sb.append(board[i][j]);
}
}
String s = sb.toString();
if ("123450".equals(s)) return 0;
int step = 0;
Deque<String> queue = new ArrayDeque<>();
queue.offer(s);
Set<String> visited = new HashSet<>();
visited.add(s);
while (!queue.isEmpty()) {
int size = queue.size();
++step;
for (int i = 0; i < size; ++i) {
String status = queue.poll();
for (String nextStatus : get(status)) {
if (!visited.contains(nextStatus)) {
if ("123450".equals(nextStatus)) {
return step;
}
queue.offer(nextStatus);
visited.add(nextStatus);
}
}
}
}

return -1;
}

private List<String> get(String status) {
List<String> list = new ArrayList<>();
char[] arr = status.toCharArray();
int x = status.indexOf('0');
for (int y : neighbors[x]) {
swap(arr, x, y);
list.add(new String(arr));
swap(arr, x, y);
}
return list;
}

private void swap(char[] arr, int x, int y) {
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}