Skip to content

Commit 2144825

Browse files
committed
Add solution and test-cases for problem 773
1 parent dbd4dda commit 2144825

File tree

6 files changed

+98
-22
lines changed

6 files changed

+98
-22
lines changed
4.73 KB
Loading
4.73 KB
Loading
4.78 KB
Loading

leetcode/701-800/0773.Sliding-Puzzle/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [773.Sliding Puzzle][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
On an `2 x 3` board, there are five tiles labeled from `1` to `5`, and an empty square represented by `0`. A **move** consists of choosing `0` and a 4-directionally adjacent number and swapping it.
5+
6+
The state of the board is solved if and only if the board is `[[1,2,3],[4,5,0]]`.
7+
8+
Given the puzzle board `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`.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.jpg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: board = [[1,2,3],[4,0,5]]
16+
Output: 1
17+
Explanation: Swap the 0 and the 5 in one move.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
22+
![2](./2.jpg)
1923

20-
### 思路1
21-
> ...
22-
Sliding Puzzle
23-
```go
24+
```
25+
Input: board = [[1,2,3],[5,4,0]]
26+
Output: -1
27+
Explanation: No number of moves will make the board solved.
2428
```
2529

30+
**Example 3:**
31+
32+
![3](./3.jpg)
33+
34+
```
35+
Input: board = [[4,1,2],[5,0,3]]
36+
Output: 5
37+
Explanation: 5 is the smallest number of moves that solves the board.
38+
An example path:
39+
After move 0: [[4,1,2],[5,0,3]]
40+
After move 1: [[4,1,2],[0,5,3]]
41+
After move 2: [[0,1,2],[4,5,3]]
42+
After move 3: [[1,0,2],[4,5,3]]
43+
After move 4: [[1,2,0],[4,5,3]]
44+
After move 5: [[1,2,3],[4,5,0]]
45+
```
2646

2747
## 结语
2848

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func isOk773(board [2][3]int) bool {
4+
return board[0][0] == 1 && board[0][1] == 2 && board[0][2] == 3 &&
5+
board[1][0] == 4 && board[1][1] == 5
6+
}
7+
8+
type qItem773 struct {
9+
key [2][3]int
10+
zeroIndex [2]int
11+
}
12+
13+
var dir773 = [4][2]int{
14+
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
15+
}
16+
17+
func Solution(board [][]int) int {
18+
19+
key := [2][3]int{
20+
{board[0][0], board[0][1], board[0][2]},
21+
{board[1][0], board[1][1], board[1][2]},
22+
}
23+
zeroIndex := [2]int{0, 0}
24+
for i := range 2 {
25+
for j := range 3 {
26+
if board[i][j] == 0 {
27+
zeroIndex = [2]int{i, j}
28+
}
29+
}
30+
}
31+
queue := []qItem773{
32+
{key, zeroIndex},
33+
}
34+
used := map[[2][3]int]struct{}{
35+
key: struct{}{},
36+
}
37+
steps := 0
38+
for len(queue) > 0 {
39+
nq := make([]qItem773, 0)
40+
for _, cur := range queue {
41+
if isOk773(cur.key) {
42+
return steps
43+
}
44+
x, y := cur.zeroIndex[0], cur.zeroIndex[1]
45+
for _, d := range dir773 {
46+
nx, ny := x+d[0], y+d[1]
47+
if nx >= 0 && nx <= 1 && ny >= 0 && ny <= 2 {
48+
b := cur.key
49+
b[nx][ny], b[x][y] = b[x][y], b[nx][ny]
50+
if _, ok := used[b]; !ok {
51+
nq = append(nq, qItem773{b, [2]int{nx, ny}})
52+
used[b] = struct{}{}
53+
}
54+
}
55+
}
56+
}
57+
queue = nq
58+
steps++
59+
}
60+
return -1
561
}

leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 2, 3}, {4, 0, 5}}, 1},
17+
{"TestCase2", [][]int{{1, 2, 3}, {5, 4, 0}}, -1},
18+
{"TestCase3", [][]int{{4, 1, 2}, {5, 0, 3}}, 5},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)