Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit c216ab8

Browse files
aQuaaQua
authored andcommitted
773 added
1 parent c1538cc commit c216ab8

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [773. Sliding Puzzle](https://leetcode.com/problems/sliding-puzzle/)
2+
3+
## 题目
4+
5+
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.
6+
7+
A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
8+
9+
The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
10+
11+
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.
12+
13+
Examples:
14+
15+
```text
16+
Input: board = [[1,2,3],[4,0,5]]
17+
Output: 1
18+
Explanation: Swap the 0 and the 5 in one move.
19+
20+
Input: board = [[1,2,3],[5,4,0]]
21+
Output: -1
22+
Explanation: No number of moves will make the board solved.
23+
24+
Input: board = [[4,1,2],[5,0,3]]
25+
Output: 5
26+
Explanation: 5 is the smallest number of moves that solves the board.
27+
An example path:
28+
After move 0: [[4,1,2],[5,0,3]]
29+
After move 1: [[4,1,2],[0,5,3]]
30+
After move 2: [[0,1,2],[4,5,3]]
31+
After move 3: [[1,0,2],[4,5,3]]
32+
After move 4: [[1,2,0],[4,5,3]]
33+
After move 5: [[1,2,3],[4,5,0]]
34+
35+
Input: board = [[3,2,4],[1,5,0]]
36+
Output: 14
37+
```
38+
39+
Note:
40+
41+
1. board will be a 2 x 3 array as described above.
42+
1. board[i][j] will be a permutation of [0, 1, 2, 3, 4, 5].
43+
44+
## 解题思路
45+
46+
见程序注释
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Problem0773
2+
3+
func slidingPuzzle(board [][]int) int {
4+
5+
return 0
6+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Problem0773
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
board [][]int
13+
ans int
14+
}{
15+
16+
{
17+
[][]int{{1, 2, 3}, {4, 0, 5}},
18+
1,
19+
},
20+
21+
{
22+
[][]int{{1, 2, 3}, {5, 4, 0}},
23+
-1,
24+
},
25+
26+
{
27+
[][]int{{4, 1, 2}, {5, 0, 3}},
28+
5,
29+
},
30+
31+
{
32+
[][]int{{3, 2, 4}, {1, 5, 0}},
33+
14,
34+
},
35+
36+
// 可以有多个 testcase
37+
}
38+
39+
func Test_slidingPuzzle(t *testing.T) {
40+
ast := assert.New(t)
41+
42+
for _, tc := range tcs {
43+
fmt.Printf("~~%v~~\n", tc)
44+
ast.Equal(tc.ans, slidingPuzzle(tc.board), "输入:%v", tc)
45+
}
46+
}
47+
48+
func Benchmark_slidingPuzzle(b *testing.B) {
49+
for i := 0; i < b.N; i++ {
50+
for _, tc := range tcs {
51+
slidingPuzzle(tc.board)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)