Skip to content

Commit fdc2abe

Browse files
add 773
1 parent 0bd6aa0 commit fdc2abe

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ _If you like this project, please leave me a star._ ★
745745
| 779 | [K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | | Medium |
746746
| 776 | [Split BST](https://leetcode.com/problems/split-bst/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | | Medium | Recursion
747747
| 775 | [Global and Local Inversions](https://leetcode.com/problems/global-and-local-inversions/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_775.java) | | Medium | Array, Math
748+
| 773 | [Sliding Puzzle](https://leetcode.com/problems/sliding-puzzle/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_773.java) | | Hard | BFS
748749
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | | Easy |
749750
| 769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_769.java) | | Medium | Array
750751
| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | | Medium |

Diff for: src/main/java/com/fishercoder/solutions/_773.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.util.Set;
7+
8+
public class _773 {
9+
public static class Solution1 {
10+
public int slidingPuzzle(int[][] board) {
11+
String target = "123450";
12+
StringBuilder sb = new StringBuilder();
13+
for (int i = 0; i < 2; i++) {
14+
for (int j = 0; j < 3; j++) {
15+
sb.append(board[i][j]);
16+
}
17+
}
18+
Queue<String> q = new LinkedList<>();
19+
String start = sb.toString();
20+
q.offer(start);
21+
Set<String> visited = new HashSet<>();
22+
visited.add(start);
23+
//since there are only 6 cells, we just use 0 through 5 to represent the positions:
24+
//0, 1, 2
25+
//3, 4, 5
26+
//the swap positions, go from left to right, top to bottom
27+
//swap[index] means the possible positions to swap when '0' is at position index
28+
int[][] swap = new int[][]{
29+
{1, 3},
30+
{0, 4, 2},
31+
{1, 5},
32+
{0, 4},
33+
{3, 1, 5},
34+
{2, 4}
35+
};
36+
int level = 0;
37+
while (!q.isEmpty()) {
38+
int size = q.size();
39+
for (int i = 0; i < size; i++) {
40+
String curr = q.poll();
41+
if (curr.equals(target)) {
42+
return level;
43+
}
44+
int index = curr.indexOf('0');
45+
for (int swapIndex : swap[index]) {
46+
sb.setLength(0);
47+
sb.append(curr);
48+
49+
//swap
50+
sb.setCharAt(index, curr.charAt(swapIndex));
51+
sb.setCharAt(swapIndex, '0');
52+
53+
String path = sb.toString();
54+
if (!visited.add(path)) {
55+
continue;
56+
}
57+
q.offer(path);
58+
}
59+
}
60+
level++;
61+
}
62+
return -1;
63+
}
64+
}
65+
}

Diff for: src/test/java/com/fishercoder/_773Test.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._773;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _773Test {
10+
private static _773.Solution1 solution1;
11+
private static int[][] board;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _773.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
board = new int[][]{
21+
{1, 2, 3},
22+
{4, 0, 5}
23+
};
24+
assertEquals(1, solution1.slidingPuzzle(board));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
board = new int[][]{
30+
{4, 1, 2},
31+
{5, 0, 3}
32+
};
33+
assertEquals(5, solution1.slidingPuzzle(board));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)