diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md index 8c41e863b0fa6..dc3cbf9134b98 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md @@ -79,7 +79,15 @@ tags: -### 方法一 +### 方法一:BFS + +我们可以从入口开始,进行广度优先搜索,每次搜索到一个新的空格子,就将其标记为已访问,并将其加入队列,直到找到一个边界上的空格子,返回步数。 + +具体地,我们定义一个队列 $q$,初始时我们将 $\textit{entrance}$ 加入队列。定义一个变量 $\textit{ans}$ 记录步数,初始为 $1$。然后我们开始进行广度优先搜索,每一轮我们取出队列中的所有元素,遍历这些元素,对于每个元素,我们尝试向四个方向移动,如果移动后的位置是一个空格子,我们将其加入队列,并将其标记为已访问。如果移动后的位置是边界上的空格子,我们返回 $\textit{ans}$。如果队列为空,我们返回 $-1$。这一轮搜索结束后,我们将 $\textit{ans}$ 加一,继续进行下一轮搜索。 + +遍历结束后,如果我们没有找到边界上的空格子,我们返回 $-1$。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是迷宫的行数和列数。 @@ -91,7 +99,7 @@ class Solution: m, n = len(maze), len(maze[0]) i, j = entrance q = deque([(i, j)]) - maze[i][j] = '+' + maze[i][j] = "+" ans = 0 while q: ans += 1 @@ -99,11 +107,11 @@ class Solution: i, j = q.popleft() for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]: x, y = i + a, j + b - if 0 <= x < m and 0 <= y < n and maze[x][y] == '.': + if 0 <= x < m and 0 <= y < n and maze[x][y] == ".": if x == 0 or x == m - 1 or y == 0 or y == n - 1: return ans q.append((x, y)) - maze[x][y] = '+' + maze[x][y] = "+" return -1 ``` @@ -112,26 +120,22 @@ class Solution: ```java class Solution { public int nearestExit(char[][] maze, int[] entrance) { - int m = maze.length; - int n = maze[0].length; + int m = maze.length, n = maze[0].length; + final int[] dirs = {-1, 0, 1, 0, -1}; Deque q = new ArrayDeque<>(); q.offer(entrance); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - while (!q.isEmpty()) { - ++ans; + for (int ans = 1; !q.isEmpty(); ++ans) { for (int k = q.size(); k > 0; --k) { - int[] p = q.poll(); - int i = p[0], j = p[1]; - for (int l = 0; l < 4; ++l) { - int x = i + dirs[l], y = j + dirs[l + 1]; + var p = q.poll(); + for (int d = 0; d < 4; ++d) { + int x = p[0] + dirs[d], y = p[1] + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { return ans; } - q.offer(new int[] {x, y}); maze[x][y] = '+'; + q.offer(new int[] {x, y}); } } } @@ -148,21 +152,22 @@ class Solution { public: int nearestExit(vector>& maze, vector& entrance) { int m = maze.size(), n = maze[0].size(); - queue> q{{entrance}}; + int dirs[5] = {-1, 0, 1, 0, -1}; + queue> q; + q.emplace(entrance[0], entrance[1]); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) { - ++ans; - for (int k = q.size(); k > 0; --k) { - auto p = q.front(); + for (int ans = 1; !q.empty(); ++ans) { + for (int k = q.size(); k; --k) { + auto [i, j] = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) { - int x = p[0] + dirs[l], y = p[1] + dirs[l + 1]; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { - if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans; - q.push({x, y}); + if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { + return ans; + } maze[x][y] = '+'; + q.emplace(x, y); } } } @@ -177,12 +182,10 @@ public: ```go func nearestExit(maze [][]byte, entrance []int) int { m, n := len(maze), len(maze[0]) - q := [][]int{entrance} + q := [][2]int{{entrance[0], entrance[1]}} maze[entrance[0]][entrance[1]] = '+' - ans := 0 dirs := []int{-1, 0, 1, 0, -1} - for len(q) > 0 { - ans++ + for ans := 1; len(q) > 0; ans++ { for k := len(q); k > 0; k-- { p := q[0] q = q[1:] @@ -192,7 +195,7 @@ func nearestExit(maze [][]byte, entrance []int) int { if x == 0 || x == m-1 || y == 0 || y == n-1 { return ans } - q = append(q, []int{x, y}) + q = append(q, [2]int{x, y}) maze[x][y] = '+' } } @@ -206,8 +209,6 @@ func nearestExit(maze [][]byte, entrance []int) int { ```ts function nearestExit(maze: string[][], entrance: number[]): number { - const m = maze.length; - const n = maze[0].length; const dir = [0, 1, 0, -1, 0]; const q = [[...entrance, 0]]; maze[entrance[0]][entrance[1]] = '+'; diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md index 3241063ee8508..be40a5147a643 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md @@ -80,7 +80,15 @@ Thus, the nearest exit is [1,2], which is 2 steps away. -### Solution 1 +### Solution 1: BFS + +We can start from the entrance and perform a breadth-first search (BFS). Each time we reach a new empty cell, we mark it as visited and add it to the queue until we find an empty cell on the boundary, then return the number of steps. + +Specifically, we define a queue $q$, initially adding $\textit{entrance}$ to the queue. We define a variable $\textit{ans}$ to record the number of steps, initially set to $1$. Then we start the BFS. In each round, we take out all elements from the queue and traverse them. For each element, we try to move in four directions. If the new position is an empty cell, we add it to the queue and mark it as visited. If the new position is an empty cell on the boundary, we return $\textit{ans}$. If the queue is empty, we return $-1$. After this round of search, we increment $\textit{ans}$ by one and continue to the next round of search. + +If we finish the traversal without finding an empty cell on the boundary, we return $-1$. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns in the maze, respectively. @@ -92,7 +100,7 @@ class Solution: m, n = len(maze), len(maze[0]) i, j = entrance q = deque([(i, j)]) - maze[i][j] = '+' + maze[i][j] = "+" ans = 0 while q: ans += 1 @@ -100,11 +108,11 @@ class Solution: i, j = q.popleft() for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]: x, y = i + a, j + b - if 0 <= x < m and 0 <= y < n and maze[x][y] == '.': + if 0 <= x < m and 0 <= y < n and maze[x][y] == ".": if x == 0 or x == m - 1 or y == 0 or y == n - 1: return ans q.append((x, y)) - maze[x][y] = '+' + maze[x][y] = "+" return -1 ``` @@ -113,26 +121,22 @@ class Solution: ```java class Solution { public int nearestExit(char[][] maze, int[] entrance) { - int m = maze.length; - int n = maze[0].length; + int m = maze.length, n = maze[0].length; + final int[] dirs = {-1, 0, 1, 0, -1}; Deque q = new ArrayDeque<>(); q.offer(entrance); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - while (!q.isEmpty()) { - ++ans; + for (int ans = 1; !q.isEmpty(); ++ans) { for (int k = q.size(); k > 0; --k) { - int[] p = q.poll(); - int i = p[0], j = p[1]; - for (int l = 0; l < 4; ++l) { - int x = i + dirs[l], y = j + dirs[l + 1]; + var p = q.poll(); + for (int d = 0; d < 4; ++d) { + int x = p[0] + dirs[d], y = p[1] + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { return ans; } - q.offer(new int[] {x, y}); maze[x][y] = '+'; + q.offer(new int[] {x, y}); } } } @@ -149,21 +153,22 @@ class Solution { public: int nearestExit(vector>& maze, vector& entrance) { int m = maze.size(), n = maze[0].size(); - queue> q{{entrance}}; + int dirs[5] = {-1, 0, 1, 0, -1}; + queue> q; + q.emplace(entrance[0], entrance[1]); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) { - ++ans; - for (int k = q.size(); k > 0; --k) { - auto p = q.front(); + for (int ans = 1; !q.empty(); ++ans) { + for (int k = q.size(); k; --k) { + auto [i, j] = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) { - int x = p[0] + dirs[l], y = p[1] + dirs[l + 1]; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { - if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans; - q.push({x, y}); + if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { + return ans; + } maze[x][y] = '+'; + q.emplace(x, y); } } } @@ -178,12 +183,10 @@ public: ```go func nearestExit(maze [][]byte, entrance []int) int { m, n := len(maze), len(maze[0]) - q := [][]int{entrance} + q := [][2]int{{entrance[0], entrance[1]}} maze[entrance[0]][entrance[1]] = '+' - ans := 0 dirs := []int{-1, 0, 1, 0, -1} - for len(q) > 0 { - ans++ + for ans := 1; len(q) > 0; ans++ { for k := len(q); k > 0; k-- { p := q[0] q = q[1:] @@ -193,7 +196,7 @@ func nearestExit(maze [][]byte, entrance []int) int { if x == 0 || x == m-1 || y == 0 || y == n-1 { return ans } - q = append(q, []int{x, y}) + q = append(q, [2]int{x, y}) maze[x][y] = '+' } } @@ -207,8 +210,6 @@ func nearestExit(maze [][]byte, entrance []int) int { ```ts function nearestExit(maze: string[][], entrance: number[]): number { - const m = maze.length; - const n = maze[0].length; const dir = [0, 1, 0, -1, 0]; const q = [[...entrance, 0]]; maze[entrance[0]][entrance[1]] = '+'; diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp index 250dd090b5edd..4260aa4da11e3 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp @@ -2,25 +2,26 @@ class Solution { public: int nearestExit(vector>& maze, vector& entrance) { int m = maze.size(), n = maze[0].size(); - queue> q{{entrance}}; + int dirs[5] = {-1, 0, 1, 0, -1}; + queue> q; + q.emplace(entrance[0], entrance[1]); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) { - ++ans; - for (int k = q.size(); k > 0; --k) { - auto p = q.front(); + for (int ans = 1; !q.empty(); ++ans) { + for (int k = q.size(); k; --k) { + auto [i, j] = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) { - int x = p[0] + dirs[l], y = p[1] + dirs[l + 1]; + for (int d = 0; d < 4; ++d) { + int x = i + dirs[d], y = j + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { - if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans; - q.push({x, y}); + if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { + return ans; + } maze[x][y] = '+'; + q.emplace(x, y); } } } } return -1; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.go b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.go index 0f8419106a379..34ec74807d5ff 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.go +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.go @@ -1,11 +1,9 @@ func nearestExit(maze [][]byte, entrance []int) int { m, n := len(maze), len(maze[0]) - q := [][]int{entrance} + q := [][2]int{{entrance[0], entrance[1]}} maze[entrance[0]][entrance[1]] = '+' - ans := 0 dirs := []int{-1, 0, 1, 0, -1} - for len(q) > 0 { - ans++ + for ans := 1; len(q) > 0; ans++ { for k := len(q); k > 0; k-- { p := q[0] q = q[1:] @@ -15,11 +13,11 @@ func nearestExit(maze [][]byte, entrance []int) int { if x == 0 || x == m-1 || y == 0 || y == n-1 { return ans } - q = append(q, []int{x, y}) + q = append(q, [2]int{x, y}) maze[x][y] = '+' } } } } return -1 -} \ No newline at end of file +} diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.java b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.java index ed02e8c800f15..709c5295f0e7a 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.java +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.java @@ -1,29 +1,25 @@ class Solution { public int nearestExit(char[][] maze, int[] entrance) { - int m = maze.length; - int n = maze[0].length; + int m = maze.length, n = maze[0].length; + final int[] dirs = {-1, 0, 1, 0, -1}; Deque q = new ArrayDeque<>(); q.offer(entrance); maze[entrance[0]][entrance[1]] = '+'; - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - while (!q.isEmpty()) { - ++ans; + for (int ans = 1; !q.isEmpty(); ++ans) { for (int k = q.size(); k > 0; --k) { - int[] p = q.poll(); - int i = p[0], j = p[1]; - for (int l = 0; l < 4; ++l) { - int x = i + dirs[l], y = j + dirs[l + 1]; + var p = q.poll(); + for (int d = 0; d < 4; ++d) { + int x = p[0] + dirs[d], y = p[1] + dirs[d + 1]; if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { if (x == 0 || x == m - 1 || y == 0 || y == n - 1) { return ans; } - q.offer(new int[] {x, y}); maze[x][y] = '+'; + q.offer(new int[] {x, y}); } } } } return -1; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.py b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.py index 8e2d374d8ac1d..eccf79574d5e9 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.py +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.py @@ -3,7 +3,7 @@ def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int: m, n = len(maze), len(maze[0]) i, j = entrance q = deque([(i, j)]) - maze[i][j] = '+' + maze[i][j] = "+" ans = 0 while q: ans += 1 @@ -11,9 +11,9 @@ def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int: i, j = q.popleft() for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]: x, y = i + a, j + b - if 0 <= x < m and 0 <= y < n and maze[x][y] == '.': + if 0 <= x < m and 0 <= y < n and maze[x][y] == ".": if x == 0 or x == m - 1 or y == 0 or y == n - 1: return ans q.append((x, y)) - maze[x][y] = '+' + maze[x][y] = "+" return -1 diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.ts b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.ts index 50f97f74d81d5..41c823f9a5c2f 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.ts +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.ts @@ -1,6 +1,4 @@ function nearestExit(maze: string[][], entrance: number[]): number { - const m = maze.length; - const n = maze[0].length; const dir = [0, 1, 0, -1, 0]; const q = [[...entrance, 0]]; maze[entrance[0]][entrance[1]] = '+'; diff --git a/solution/1900-1999/1927.Sum Game/README_EN.md b/solution/1900-1999/1927.Sum Game/README_EN.md index 0f5be2c9c940a..8a16e22abac7e 100644 --- a/solution/1900-1999/1927.Sum Game/README_EN.md +++ b/solution/1900-1999/1927.Sum Game/README_EN.md @@ -86,7 +86,23 @@ Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7. -### Solution 1 +### Solution 1: Case Analysis + +If the number of `'?'` is odd, Alice will definitely win because she can choose to replace the last `'?'` with any digit, making the sum of the first half different from the sum of the second half. + +If the number of `'?'` is even, Alice will try to make the sums of the two halves different by placing $9$ in the half with the larger current sum and $0$ in the half with the smaller current sum. Bob, on the other hand, will try to make the sums equal by placing a digit in the other half that matches the digit Alice placed. + +As a result, all remaining even-numbered `'?'` will be concentrated in one half. Suppose the current difference between the sums of the two halves is $d$. + +Let's consider the case where there are two remaining `'?'` and the difference is $x$: + +- If $x \lt 9$, Alice will definitely win because she can replace one of the `'?'` with $9$, making the sums of the two halves different. +- If $x \gt 9$, Alice will definitely win because she can replace one of the `'?'` with $0$, making the sums of the two halves different. +- If $x = 9$, Bob will definitely win. Suppose Alice replaces a digit with $a$, then Bob can replace the other `'?'` with $9 - a$, making the sums of the two halves equal. + +Therefore, if the difference between the sums of the two halves is $d = \frac{9 \times \textit{cnt}}{2}$, where $\textit{cnt}$ is the number of remaining `'?'`, Bob will definitely win; otherwise, Alice will definitely win. + +The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.