Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added leetcode/701-800/0773.Sliding-Puzzle/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/701-800/0773.Sliding-Puzzle/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/701-800/0773.Sliding-Puzzle/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 33 additions & 13 deletions leetcode/701-800/0773.Sliding-Puzzle/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
# [773.Sliding Puzzle][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

The state of the board is solved if and only if the board is `[[1,2,3],[4,5,0]]`.

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`.

**Example 1:**
**Example 1:**

![1](./1.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.jpg)

### 思路1
> ...
Sliding Puzzle
```go
```
Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.
```

**Example 3:**

![3](./3.jpg)

```
Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
```

## 结语

Expand Down
60 changes: 58 additions & 2 deletions leetcode/701-800/0773.Sliding-Puzzle/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
package Solution

func Solution(x bool) bool {
return x
func isOk773(board [2][3]int) bool {
return board[0][0] == 1 && board[0][1] == 2 && board[0][2] == 3 &&
board[1][0] == 4 && board[1][1] == 5
}

type qItem773 struct {
key [2][3]int
zeroIndex [2]int
}

var dir773 = [4][2]int{
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
}

func Solution(board [][]int) int {

key := [2][3]int{
{board[0][0], board[0][1], board[0][2]},
{board[1][0], board[1][1], board[1][2]},
}
zeroIndex := [2]int{0, 0}
for i := range 2 {
for j := range 3 {
if board[i][j] == 0 {
zeroIndex = [2]int{i, j}
}
}
}
queue := []qItem773{
{key, zeroIndex},
}
used := map[[2][3]int]struct{}{
key: struct{}{},
}
steps := 0
for len(queue) > 0 {
nq := make([]qItem773, 0)
for _, cur := range queue {
if isOk773(cur.key) {
return steps
}
x, y := cur.zeroIndex[0], cur.zeroIndex[1]
for _, d := range dir773 {
nx, ny := x+d[0], y+d[1]
if nx >= 0 && nx <= 1 && ny >= 0 && ny <= 2 {
b := cur.key
b[nx][ny], b[x][y] = b[x][y], b[nx][ny]
if _, ok := used[b]; !ok {
nq = append(nq, qItem773{b, [2]int{nx, ny}})
used[b] = struct{}{}
}
}
}
}
queue = nq
steps++
}
return -1
}
14 changes: 7 additions & 7 deletions leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 2, 3}, {4, 0, 5}}, 1},
{"TestCase2", [][]int{{1, 2, 3}, {5, 4, 0}}, -1},
{"TestCase3", [][]int{{4, 1, 2}, {5, 0, 3}}, 5},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading