diff --git a/leetcode/701-800/0773.Sliding-Puzzle/1.jpg b/leetcode/701-800/0773.Sliding-Puzzle/1.jpg new file mode 100644 index 000000000..9bffca227 Binary files /dev/null and b/leetcode/701-800/0773.Sliding-Puzzle/1.jpg differ diff --git a/leetcode/701-800/0773.Sliding-Puzzle/2.jpg b/leetcode/701-800/0773.Sliding-Puzzle/2.jpg new file mode 100644 index 000000000..93f8a7efa Binary files /dev/null and b/leetcode/701-800/0773.Sliding-Puzzle/2.jpg differ diff --git a/leetcode/701-800/0773.Sliding-Puzzle/3.jpg b/leetcode/701-800/0773.Sliding-Puzzle/3.jpg new file mode 100644 index 000000000..c28f9c91e Binary files /dev/null and b/leetcode/701-800/0773.Sliding-Puzzle/3.jpg differ diff --git a/leetcode/701-800/0773.Sliding-Puzzle/README.md b/leetcode/701-800/0773.Sliding-Puzzle/README.md index d2ab9e31b..c632167f6 100644 --- a/leetcode/701-800/0773.Sliding-Puzzle/README.md +++ b/leetcode/701-800/0773.Sliding-Puzzle/README.md @@ -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]] +``` ## 结语 diff --git a/leetcode/701-800/0773.Sliding-Puzzle/Solution.go b/leetcode/701-800/0773.Sliding-Puzzle/Solution.go index d115ccf5e..7ff0c11bd 100644 --- a/leetcode/701-800/0773.Sliding-Puzzle/Solution.go +++ b/leetcode/701-800/0773.Sliding-Puzzle/Solution.go @@ -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 } diff --git a/leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go b/leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go index 14ff50eb4..2a49619ec 100644 --- a/leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go +++ b/leetcode/701-800/0773.Sliding-Puzzle/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }