-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliding-Puzzle.java
More file actions
77 lines (65 loc) · 2.29 KB
/
Copy pathSliding-Puzzle.java
File metadata and controls
77 lines (65 loc) · 2.29 KB
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
class Solution {
private static final int FINAL_STATE = 0b001010011100101000;
private static final int[][] DIRS = { { 1, 3 }, { 0, 2, 4 }, { 1, 5 }, { 0, 4 }, { 1, 3, 5 }, { 2, 4 } };
public int slidingPuzzle(int[][] board) {
if (board == null || board.length != 2 || board[0].length != 3) {
throw new IllegalArgumentException("Input board is invalid");
}
int zeroIdx = -1;
int curState = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
// Inserting the num at the end of integer
curState = (curState << 3) | board[i][j];
if (board[i][j] == 0) {
zeroIdx = i * 3 + j;
}
}
}
if (FINAL_STATE == curState) {
return 0;
}
HashSet<Integer> visited = new HashSet<>();
int moves = 0;
HashMap<Integer, Integer> begin = new HashMap<>();
begin.put(curState, zeroIdx);
visited.add(curState);
HashMap<Integer, Integer> end = new HashMap<>();
end.put(FINAL_STATE, 5);
visited.add(FINAL_STATE);
while (!begin.isEmpty()) {
if (begin.size() > end.size()) {
HashMap<Integer, Integer> tempSet = begin;
begin = end;
end = tempSet;
}
HashMap<Integer, Integer> next = new HashMap<>();
moves++;
for (int cur : begin.keySet()) {
zeroIdx = begin.get(cur);
for (int d : DIRS[zeroIdx]) {
int newState = swap(cur, zeroIdx, d);
if (end.containsKey(newState)) {
return moves;
}
if (visited.add(newState)) {
next.put(newState, d);
}
}
}
begin = next;
}
return -1;
}
private int swap(int state, int zeroIdx, int destIdx) {
int mask = 0b111 << ((5 - destIdx) * 3);
int num = state & mask;
if (zeroIdx < destIdx) {
num <<= (destIdx - zeroIdx) * 3;
} else {
num >>>= (zeroIdx - destIdx) * 3;
}
state &= ~mask;
return state | num;
}
}s