diff --git a/solution/0900-0999/0909.Snakes and Ladders/README.md b/solution/0900-0999/0909.Snakes and Ladders/README.md index 04ead417a97b5..893ea45ad6620 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/README.md +++ b/solution/0900-0999/0909.Snakes and Ladders/README.md @@ -88,6 +88,18 @@ tags: ### 方法一:BFS +我们可以使用广度优先搜索的方法,从起点开始,每次向前走 1 到 6 步,然后判断是否有蛇或梯子,如果有,就走到蛇或梯子的目的地,否则就走到下一个方格。 + +具体地,我们使用一个队列 $\textit{q}$ 来存储当前可以到达的方格编号,初始时将编号 $1$ 放入队列。同时我们使用一个集合 $\textit{vis}$ 来记录已经到达过的方格,避免重复访问,初始时将编号 $1$ 加入集合 $\textit{vis}$。 + +在每一次的操作中,我们取出队首的方格编号 $x$,如果 $x$ 是终点,那么我们就可以返回当前的步数。否则我们将 $x$ 向前走 $1$ 到 $6$ 步,设新的编号为 $y$,如果 $y$ 落在棋盘外,那么我们就直接跳过。否则,我们需要找到 $y$ 对应的行和列,由于行的编号是从下到上递减的,而列的编号与行的奇偶性有关,因此我们需要进行一些计算得到 $y$ 对应的行和列。 + +如果 $y$ 对应的方格上有蛇或梯子,那么我们需要额外走到蛇或梯子的目的地,设其为 $z$。如果 $z$ 没有被访问过,我们就将 $z$ 加入队列和集合中,这样我们就可以继续进行广度优先搜索。 + +如果我们最终无法到达终点,那么我们就返回 $-1$。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是棋盘的边长。 + #### Python3 @@ -95,28 +107,25 @@ tags: ```python class Solution: def snakesAndLadders(self, board: List[List[int]]) -> int: - def get(x): - i, j = (x - 1) // n, (x - 1) % n - if i & 1: - j = n - 1 - j - return n - 1 - i, j - n = len(board) q = deque([1]) vis = {1} ans = 0 + m = n * n while q: for _ in range(len(q)): - curr = q.popleft() - if curr == n * n: + x = q.popleft() + if x == m: return ans - for next in range(curr + 1, min(curr + 7, n * n + 1)): - i, j = get(next) - if board[i][j] != -1: - next = board[i][j] - if next not in vis: - q.append(next) - vis.add(next) + for y in range(x + 1, min(x + 6, m) + 1): + i, j = divmod(y - 1, n) + if i & 1: + j = n - j - 1 + i = n - i - 1 + z = y if board[i][j] == -1 else board[i][j] + if z not in vis: + vis.add(z) + q.append(z) ans += 1 return -1 ``` @@ -125,46 +134,35 @@ class Solution: ```java class Solution { - private int n; - public int snakesAndLadders(int[][] board) { - n = board.length; + int n = board.length; Deque q = new ArrayDeque<>(); q.offer(1); - boolean[] vis = new boolean[n * n + 1]; + int m = n * n; + boolean[] vis = new boolean[m + 1]; vis[1] = true; - int ans = 0; - while (!q.isEmpty()) { - for (int t = q.size(); t > 0; --t) { - int curr = q.poll(); - if (curr == n * n) { + for (int ans = 0; !q.isEmpty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.poll(); + if (x == m) { return ans; } - for (int k = curr + 1; k <= Math.min(curr + 6, n * n); ++k) { - int[] p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) { - next = board[i][j]; + for (int y = x + 1; y <= Math.min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; } - if (!vis[next]) { - vis[next] = true; - q.offer(next); + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.offer(z); } } } - ++ans; } return -1; } - - private int[] get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) { - j = n - 1 - j; - } - return new int[] {n - 1 - i, j}; - } } ``` @@ -173,40 +171,36 @@ class Solution { ```cpp class Solution { public: - int n; - int snakesAndLadders(vector>& board) { - n = board.size(); + int n = board.size(); queue q{{1}}; - vector vis(n * n + 1); + int m = n * n; + vector vis(m + 1); vis[1] = true; - int ans = 0; - while (!q.empty()) { - for (int t = q.size(); t; --t) { - int curr = q.front(); - if (curr == n * n) return ans; + + for (int ans = 0; !q.empty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.front(); q.pop(); - for (int k = curr + 1; k <= min(curr + 6, n * n); ++k) { - auto p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) next = board[i][j]; - if (!vis[next]) { - vis[next] = true; - q.push(next); + if (x == m) { + return ans; + } + for (int y = x + 1; y <= min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; + } + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.push(z); } } } - ++ans; } return -1; } - - vector get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) j = n - 1 - j; - return {n - 1 - i, j}; - } }; ``` @@ -215,43 +209,78 @@ public: ```go func snakesAndLadders(board [][]int) int { n := len(board) - get := func(x int) []int { - i, j := (x-1)/n, (x-1)%n - if i%2 == 1 { - j = n - 1 - j - } - return []int{n - 1 - i, j} - } q := []int{1} - vis := make([]bool, n*n+1) + m := n * n + vis := make([]bool, m+1) vis[1] = true - ans := 0 - for len(q) > 0 { - for t := len(q); t > 0; t-- { - curr := q[0] - if curr == n*n { + + for ans := 0; len(q) > 0; ans++ { + for k := len(q); k > 0; k-- { + x := q[0] + q = q[1:] + if x == m { return ans } - q = q[1:] - for k := curr + 1; k <= curr+6 && k <= n*n; k++ { - p := get(k) - next := k - i, j := p[0], p[1] + for y := x + 1; y <= min(x+6, m); y++ { + i, j := (y-1)/n, (y-1)%n + if i%2 == 1 { + j = n - j - 1 + } + i = n - i - 1 + z := y if board[i][j] != -1 { - next = board[i][j] + z = board[i][j] } - if !vis[next] { - vis[next] = true - q = append(q, next) + if !vis[z] { + vis[z] = true + q = append(q, z) } } } - ans++ } return -1 } ``` +#### TypeScript + +```ts +function snakesAndLadders(board: number[][]): number { + const n = board.length; + const q: number[] = [1]; + const m = n * n; + const vis: boolean[] = Array(m + 1).fill(false); + vis[1] = true; + + for (let ans = 0; q.length > 0; ans++) { + const nq: number[] = []; + for (const x of q) { + if (x === m) { + return ans; + } + for (let y = x + 1; y <= Math.min(x + 6, m); y++) { + let i = Math.floor((y - 1) / n); + let j = (y - 1) % n; + if (i % 2 === 1) { + j = n - j - 1; + } + i = n - i - 1; + const z = board[i][j] === -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + nq.push(z); + } + } + } + q.length = 0; + for (const x of nq) { + q.push(x); + } + } + return -1; +} +``` + diff --git a/solution/0900-0999/0909.Snakes and Ladders/README_EN.md b/solution/0900-0999/0909.Snakes and Ladders/README_EN.md index 52e30c96332a6..f7a4a4219b138 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/README_EN.md +++ b/solution/0900-0999/0909.Snakes and Ladders/README_EN.md @@ -82,7 +82,19 @@ This is the lowest possible number of moves to reach the last square, so return -### Solution 1 +### Solution 1: BFS + +We can use the Breadth-First Search (BFS) method, starting from the starting point, moving forward 1 to 6 steps each time, and then checking for snakes or ladders. If there are any, move to the destination of the snake or ladder; otherwise, move to the next square. + +Specifically, we use a queue $\textit{q}$ to store the current reachable square numbers, initially putting number $1$ into the queue. At the same time, we use a set $\textit{vis}$ to record the squares that have been reached to avoid revisiting them, initially adding number $1$ to the set $\textit{vis}$. + +In each operation, we take out the square number $x$ at the front of the queue. If $x$ is the endpoint, we can return the current number of steps. Otherwise, we move $x$ forward 1 to 6 steps, setting the new number as $y$. If $y$ falls outside the board, we skip it directly. Otherwise, we need to find the row and column corresponding to $y$. Since the row numbers decrease from bottom to top, and the column numbers depend on the parity of the row, we need to perform some calculations to get the row and column corresponding to $y$. + +If the square corresponding to $y$ has a snake or ladder, we need to move to the destination of the snake or ladder, denoted as $z$. If $z$ has not been visited, we add $z$ to the queue and set, allowing us to continue the breadth-first search. + +If we ultimately cannot reach the endpoint, we return $-1$. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the side of the board. @@ -91,28 +103,25 @@ This is the lowest possible number of moves to reach the last square, so return ```python class Solution: def snakesAndLadders(self, board: List[List[int]]) -> int: - def get(x): - i, j = (x - 1) // n, (x - 1) % n - if i & 1: - j = n - 1 - j - return n - 1 - i, j - n = len(board) q = deque([1]) vis = {1} ans = 0 + m = n * n while q: for _ in range(len(q)): - curr = q.popleft() - if curr == n * n: + x = q.popleft() + if x == m: return ans - for next in range(curr + 1, min(curr + 7, n * n + 1)): - i, j = get(next) - if board[i][j] != -1: - next = board[i][j] - if next not in vis: - q.append(next) - vis.add(next) + for y in range(x + 1, min(x + 6, m) + 1): + i, j = divmod(y - 1, n) + if i & 1: + j = n - j - 1 + i = n - i - 1 + z = y if board[i][j] == -1 else board[i][j] + if z not in vis: + vis.add(z) + q.append(z) ans += 1 return -1 ``` @@ -121,46 +130,35 @@ class Solution: ```java class Solution { - private int n; - public int snakesAndLadders(int[][] board) { - n = board.length; + int n = board.length; Deque q = new ArrayDeque<>(); q.offer(1); - boolean[] vis = new boolean[n * n + 1]; + int m = n * n; + boolean[] vis = new boolean[m + 1]; vis[1] = true; - int ans = 0; - while (!q.isEmpty()) { - for (int t = q.size(); t > 0; --t) { - int curr = q.poll(); - if (curr == n * n) { + for (int ans = 0; !q.isEmpty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.poll(); + if (x == m) { return ans; } - for (int k = curr + 1; k <= Math.min(curr + 6, n * n); ++k) { - int[] p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) { - next = board[i][j]; + for (int y = x + 1; y <= Math.min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; } - if (!vis[next]) { - vis[next] = true; - q.offer(next); + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.offer(z); } } } - ++ans; } return -1; } - - private int[] get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) { - j = n - 1 - j; - } - return new int[] {n - 1 - i, j}; - } } ``` @@ -169,40 +167,36 @@ class Solution { ```cpp class Solution { public: - int n; - int snakesAndLadders(vector>& board) { - n = board.size(); + int n = board.size(); queue q{{1}}; - vector vis(n * n + 1); + int m = n * n; + vector vis(m + 1); vis[1] = true; - int ans = 0; - while (!q.empty()) { - for (int t = q.size(); t; --t) { - int curr = q.front(); - if (curr == n * n) return ans; + + for (int ans = 0; !q.empty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.front(); q.pop(); - for (int k = curr + 1; k <= min(curr + 6, n * n); ++k) { - auto p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) next = board[i][j]; - if (!vis[next]) { - vis[next] = true; - q.push(next); + if (x == m) { + return ans; + } + for (int y = x + 1; y <= min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; + } + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.push(z); } } } - ++ans; } return -1; } - - vector get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) j = n - 1 - j; - return {n - 1 - i, j}; - } }; ``` @@ -211,43 +205,78 @@ public: ```go func snakesAndLadders(board [][]int) int { n := len(board) - get := func(x int) []int { - i, j := (x-1)/n, (x-1)%n - if i%2 == 1 { - j = n - 1 - j - } - return []int{n - 1 - i, j} - } q := []int{1} - vis := make([]bool, n*n+1) + m := n * n + vis := make([]bool, m+1) vis[1] = true - ans := 0 - for len(q) > 0 { - for t := len(q); t > 0; t-- { - curr := q[0] - if curr == n*n { + + for ans := 0; len(q) > 0; ans++ { + for k := len(q); k > 0; k-- { + x := q[0] + q = q[1:] + if x == m { return ans } - q = q[1:] - for k := curr + 1; k <= curr+6 && k <= n*n; k++ { - p := get(k) - next := k - i, j := p[0], p[1] + for y := x + 1; y <= min(x+6, m); y++ { + i, j := (y-1)/n, (y-1)%n + if i%2 == 1 { + j = n - j - 1 + } + i = n - i - 1 + z := y if board[i][j] != -1 { - next = board[i][j] + z = board[i][j] } - if !vis[next] { - vis[next] = true - q = append(q, next) + if !vis[z] { + vis[z] = true + q = append(q, z) } } } - ans++ } return -1 } ``` +#### TypeScript + +```ts +function snakesAndLadders(board: number[][]): number { + const n = board.length; + const q: number[] = [1]; + const m = n * n; + const vis: boolean[] = Array(m + 1).fill(false); + vis[1] = true; + + for (let ans = 0; q.length > 0; ans++) { + const nq: number[] = []; + for (const x of q) { + if (x === m) { + return ans; + } + for (let y = x + 1; y <= Math.min(x + 6, m); y++) { + let i = Math.floor((y - 1) / n); + let j = (y - 1) % n; + if (i % 2 === 1) { + j = n - j - 1; + } + i = n - i - 1; + const z = board[i][j] === -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + nq.push(z); + } + } + } + q.length = 0; + for (const x of nq) { + q.push(x); + } + } + return -1; +} +``` + diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp b/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp index c3a2ceaee14aa..8104ac1f27dcb 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp @@ -1,37 +1,33 @@ class Solution { public: - int n; - int snakesAndLadders(vector>& board) { - n = board.size(); + int n = board.size(); queue q{{1}}; - vector vis(n * n + 1); + int m = n * n; + vector vis(m + 1); vis[1] = true; - int ans = 0; - while (!q.empty()) { - for (int t = q.size(); t; --t) { - int curr = q.front(); - if (curr == n * n) return ans; + + for (int ans = 0; !q.empty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.front(); q.pop(); - for (int k = curr + 1; k <= min(curr + 6, n * n); ++k) { - auto p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) next = board[i][j]; - if (!vis[next]) { - vis[next] = true; - q.push(next); + if (x == m) { + return ans; + } + for (int y = x + 1; y <= min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; + } + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.push(z); } } } - ++ans; } return -1; } - - vector get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) j = n - 1 - j; - return {n - 1 - i, j}; - } }; \ No newline at end of file diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.go b/solution/0900-0999/0909.Snakes and Ladders/Solution.go index 01f83480a5704..e490911e1b196 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/Solution.go +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.go @@ -1,37 +1,33 @@ func snakesAndLadders(board [][]int) int { n := len(board) - get := func(x int) []int { - i, j := (x-1)/n, (x-1)%n - if i%2 == 1 { - j = n - 1 - j - } - return []int{n - 1 - i, j} - } q := []int{1} - vis := make([]bool, n*n+1) + m := n * n + vis := make([]bool, m+1) vis[1] = true - ans := 0 - for len(q) > 0 { - for t := len(q); t > 0; t-- { - curr := q[0] - if curr == n*n { + + for ans := 0; len(q) > 0; ans++ { + for k := len(q); k > 0; k-- { + x := q[0] + q = q[1:] + if x == m { return ans } - q = q[1:] - for k := curr + 1; k <= curr+6 && k <= n*n; k++ { - p := get(k) - next := k - i, j := p[0], p[1] + for y := x + 1; y <= min(x+6, m); y++ { + i, j := (y-1)/n, (y-1)%n + if i%2 == 1 { + j = n - j - 1 + } + i = n - i - 1 + z := y if board[i][j] != -1 { - next = board[i][j] + z = board[i][j] } - if !vis[next] { - vis[next] = true - q = append(q, next) + if !vis[z] { + vis[z] = true + q = append(q, z) } } } - ans++ } return -1 } \ No newline at end of file diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.java b/solution/0900-0999/0909.Snakes and Ladders/Solution.java index e4b7fa0dd490a..e37753f7ba04c 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/Solution.java +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.java @@ -1,42 +1,31 @@ class Solution { - private int n; - public int snakesAndLadders(int[][] board) { - n = board.length; + int n = board.length; Deque q = new ArrayDeque<>(); q.offer(1); - boolean[] vis = new boolean[n * n + 1]; + int m = n * n; + boolean[] vis = new boolean[m + 1]; vis[1] = true; - int ans = 0; - while (!q.isEmpty()) { - for (int t = q.size(); t > 0; --t) { - int curr = q.poll(); - if (curr == n * n) { + for (int ans = 0; !q.isEmpty(); ++ans) { + for (int k = q.size(); k > 0; --k) { + int x = q.poll(); + if (x == m) { return ans; } - for (int k = curr + 1; k <= Math.min(curr + 6, n * n); ++k) { - int[] p = get(k); - int next = k; - int i = p[0], j = p[1]; - if (board[i][j] != -1) { - next = board[i][j]; + for (int y = x + 1; y <= Math.min(x + 6, m); ++y) { + int i = (y - 1) / n, j = (y - 1) % n; + if (i % 2 == 1) { + j = n - j - 1; } - if (!vis[next]) { - vis[next] = true; - q.offer(next); + i = n - i - 1; + int z = board[i][j] == -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + q.offer(z); } } } - ++ans; } return -1; } - - private int[] get(int x) { - int i = (x - 1) / n, j = (x - 1) % n; - if (i % 2 == 1) { - j = n - 1 - j; - } - return new int[] {n - 1 - i, j}; - } } \ No newline at end of file diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.py b/solution/0900-0999/0909.Snakes and Ladders/Solution.py index 7cdd1e15fd6a7..246b56e201303 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/Solution.py +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.py @@ -1,26 +1,23 @@ class Solution: def snakesAndLadders(self, board: List[List[int]]) -> int: - def get(x): - i, j = (x - 1) // n, (x - 1) % n - if i & 1: - j = n - 1 - j - return n - 1 - i, j - n = len(board) q = deque([1]) vis = {1} ans = 0 + m = n * n while q: for _ in range(len(q)): - curr = q.popleft() - if curr == n * n: + x = q.popleft() + if x == m: return ans - for next in range(curr + 1, min(curr + 7, n * n + 1)): - i, j = get(next) - if board[i][j] != -1: - next = board[i][j] - if next not in vis: - q.append(next) - vis.add(next) + for y in range(x + 1, min(x + 6, m) + 1): + i, j = divmod(y - 1, n) + if i & 1: + j = n - j - 1 + i = n - i - 1 + z = y if board[i][j] == -1 else board[i][j] + if z not in vis: + vis.add(z) + q.append(z) ans += 1 return -1 diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.ts b/solution/0900-0999/0909.Snakes and Ladders/Solution.ts new file mode 100644 index 0000000000000..a91038187344a --- /dev/null +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.ts @@ -0,0 +1,34 @@ +function snakesAndLadders(board: number[][]): number { + const n = board.length; + const q: number[] = [1]; + const m = n * n; + const vis: boolean[] = Array(m + 1).fill(false); + vis[1] = true; + + for (let ans = 0; q.length > 0; ans++) { + const nq: number[] = []; + for (const x of q) { + if (x === m) { + return ans; + } + for (let y = x + 1; y <= Math.min(x + 6, m); y++) { + let i = Math.floor((y - 1) / n); + let j = (y - 1) % n; + if (i % 2 === 1) { + j = n - j - 1; + } + i = n - i - 1; + const z = board[i][j] === -1 ? y : board[i][j]; + if (!vis[z]) { + vis[z] = true; + nq.push(z); + } + } + } + q.length = 0; + for (const x of nq) { + q.push(x); + } + } + return -1; +}