Skip to content

Commit eae0c1f

Browse files
authored
feat: add solutions to lc problems: No.1926,1927 (#3992)
* No.1926.Nearest Exit from Entrance in Maze * No.1927.Sum Game
1 parent a2b7b6d commit eae0c1f

File tree

8 files changed

+113
-102
lines changed

8 files changed

+113
-102
lines changed

solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README.md

+34-33
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### 方法一
82+
### 方法一:BFS
83+
84+
我们可以从入口开始,进行广度优先搜索,每次搜索到一个新的空格子,就将其标记为已访问,并将其加入队列,直到找到一个边界上的空格子,返回步数。
85+
86+
具体地,我们定义一个队列 $q$,初始时我们将 $\textit{entrance}$ 加入队列。定义一个变量 $\textit{ans}$ 记录步数,初始为 $1$。然后我们开始进行广度优先搜索,每一轮我们取出队列中的所有元素,遍历这些元素,对于每个元素,我们尝试向四个方向移动,如果移动后的位置是一个空格子,我们将其加入队列,并将其标记为已访问。如果移动后的位置是边界上的空格子,我们返回 $\textit{ans}$。如果队列为空,我们返回 $-1$。这一轮搜索结束后,我们将 $\textit{ans}$ 加一,继续进行下一轮搜索。
87+
88+
遍历结束后,如果我们没有找到边界上的空格子,我们返回 $-1$。
89+
90+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是迷宫的行数和列数。
8391

8492
<!-- tabs:start -->
8593

@@ -91,19 +99,19 @@ class Solution:
9199
m, n = len(maze), len(maze[0])
92100
i, j = entrance
93101
q = deque([(i, j)])
94-
maze[i][j] = '+'
102+
maze[i][j] = "+"
95103
ans = 0
96104
while q:
97105
ans += 1
98106
for _ in range(len(q)):
99107
i, j = q.popleft()
100108
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
101109
x, y = i + a, j + b
102-
if 0 <= x < m and 0 <= y < n and maze[x][y] == '.':
110+
if 0 <= x < m and 0 <= y < n and maze[x][y] == ".":
103111
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
104112
return ans
105113
q.append((x, y))
106-
maze[x][y] = '+'
114+
maze[x][y] = "+"
107115
return -1
108116
```
109117

@@ -112,26 +120,22 @@ class Solution:
112120
```java
113121
class Solution {
114122
public int nearestExit(char[][] maze, int[] entrance) {
115-
int m = maze.length;
116-
int n = maze[0].length;
123+
int m = maze.length, n = maze[0].length;
124+
final int[] dirs = {-1, 0, 1, 0, -1};
117125
Deque<int[]> q = new ArrayDeque<>();
118126
q.offer(entrance);
119127
maze[entrance[0]][entrance[1]] = '+';
120-
int ans = 0;
121-
int[] dirs = {-1, 0, 1, 0, -1};
122-
while (!q.isEmpty()) {
123-
++ans;
128+
for (int ans = 1; !q.isEmpty(); ++ans) {
124129
for (int k = q.size(); k > 0; --k) {
125-
int[] p = q.poll();
126-
int i = p[0], j = p[1];
127-
for (int l = 0; l < 4; ++l) {
128-
int x = i + dirs[l], y = j + dirs[l + 1];
130+
var p = q.poll();
131+
for (int d = 0; d < 4; ++d) {
132+
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
129133
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
130134
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
131135
return ans;
132136
}
133-
q.offer(new int[] {x, y});
134137
maze[x][y] = '+';
138+
q.offer(new int[] {x, y});
135139
}
136140
}
137141
}
@@ -148,21 +152,22 @@ class Solution {
148152
public:
149153
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
150154
int m = maze.size(), n = maze[0].size();
151-
queue<vector<int>> q{{entrance}};
155+
int dirs[5] = {-1, 0, 1, 0, -1};
156+
queue<pair<int, int>> q;
157+
q.emplace(entrance[0], entrance[1]);
152158
maze[entrance[0]][entrance[1]] = '+';
153-
int ans = 0;
154-
vector<int> dirs = {-1, 0, 1, 0, -1};
155-
while (!q.empty()) {
156-
++ans;
157-
for (int k = q.size(); k > 0; --k) {
158-
auto p = q.front();
159+
for (int ans = 1; !q.empty(); ++ans) {
160+
for (int k = q.size(); k; --k) {
161+
auto [i, j] = q.front();
159162
q.pop();
160-
for (int l = 0; l < 4; ++l) {
161-
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
163+
for (int d = 0; d < 4; ++d) {
164+
int x = i + dirs[d], y = j + dirs[d + 1];
162165
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
163-
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
164-
q.push({x, y});
166+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
167+
return ans;
168+
}
165169
maze[x][y] = '+';
170+
q.emplace(x, y);
166171
}
167172
}
168173
}
@@ -177,12 +182,10 @@ public:
177182
```go
178183
func nearestExit(maze [][]byte, entrance []int) int {
179184
m, n := len(maze), len(maze[0])
180-
q := [][]int{entrance}
185+
q := [][2]int{{entrance[0], entrance[1]}}
181186
maze[entrance[0]][entrance[1]] = '+'
182-
ans := 0
183187
dirs := []int{-1, 0, 1, 0, -1}
184-
for len(q) > 0 {
185-
ans++
188+
for ans := 1; len(q) > 0; ans++ {
186189
for k := len(q); k > 0; k-- {
187190
p := q[0]
188191
q = q[1:]
@@ -192,7 +195,7 @@ func nearestExit(maze [][]byte, entrance []int) int {
192195
if x == 0 || x == m-1 || y == 0 || y == n-1 {
193196
return ans
194197
}
195-
q = append(q, []int{x, y})
198+
q = append(q, [2]int{x, y})
196199
maze[x][y] = '+'
197200
}
198201
}
@@ -206,8 +209,6 @@ func nearestExit(maze [][]byte, entrance []int) int {
206209

207210
```ts
208211
function nearestExit(maze: string[][], entrance: number[]): number {
209-
const m = maze.length;
210-
const n = maze[0].length;
211212
const dir = [0, 1, 0, -1, 0];
212213
const q = [[...entrance, 0]];
213214
maze[entrance[0]][entrance[1]] = '+';

solution/1900-1999/1926.Nearest Exit from Entrance in Maze/README_EN.md

+34-33
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ Thus, the nearest exit is [1,2], which is 2 steps away.
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: BFS
84+
85+
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.
86+
87+
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.
88+
89+
If we finish the traversal without finding an empty cell on the boundary, we return $-1$.
90+
91+
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.
8492

8593
<!-- tabs:start -->
8694

@@ -92,19 +100,19 @@ class Solution:
92100
m, n = len(maze), len(maze[0])
93101
i, j = entrance
94102
q = deque([(i, j)])
95-
maze[i][j] = '+'
103+
maze[i][j] = "+"
96104
ans = 0
97105
while q:
98106
ans += 1
99107
for _ in range(len(q)):
100108
i, j = q.popleft()
101109
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
102110
x, y = i + a, j + b
103-
if 0 <= x < m and 0 <= y < n and maze[x][y] == '.':
111+
if 0 <= x < m and 0 <= y < n and maze[x][y] == ".":
104112
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
105113
return ans
106114
q.append((x, y))
107-
maze[x][y] = '+'
115+
maze[x][y] = "+"
108116
return -1
109117
```
110118

@@ -113,26 +121,22 @@ class Solution:
113121
```java
114122
class Solution {
115123
public int nearestExit(char[][] maze, int[] entrance) {
116-
int m = maze.length;
117-
int n = maze[0].length;
124+
int m = maze.length, n = maze[0].length;
125+
final int[] dirs = {-1, 0, 1, 0, -1};
118126
Deque<int[]> q = new ArrayDeque<>();
119127
q.offer(entrance);
120128
maze[entrance[0]][entrance[1]] = '+';
121-
int ans = 0;
122-
int[] dirs = {-1, 0, 1, 0, -1};
123-
while (!q.isEmpty()) {
124-
++ans;
129+
for (int ans = 1; !q.isEmpty(); ++ans) {
125130
for (int k = q.size(); k > 0; --k) {
126-
int[] p = q.poll();
127-
int i = p[0], j = p[1];
128-
for (int l = 0; l < 4; ++l) {
129-
int x = i + dirs[l], y = j + dirs[l + 1];
131+
var p = q.poll();
132+
for (int d = 0; d < 4; ++d) {
133+
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
130134
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
131135
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
132136
return ans;
133137
}
134-
q.offer(new int[] {x, y});
135138
maze[x][y] = '+';
139+
q.offer(new int[] {x, y});
136140
}
137141
}
138142
}
@@ -149,21 +153,22 @@ class Solution {
149153
public:
150154
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
151155
int m = maze.size(), n = maze[0].size();
152-
queue<vector<int>> q{{entrance}};
156+
int dirs[5] = {-1, 0, 1, 0, -1};
157+
queue<pair<int, int>> q;
158+
q.emplace(entrance[0], entrance[1]);
153159
maze[entrance[0]][entrance[1]] = '+';
154-
int ans = 0;
155-
vector<int> dirs = {-1, 0, 1, 0, -1};
156-
while (!q.empty()) {
157-
++ans;
158-
for (int k = q.size(); k > 0; --k) {
159-
auto p = q.front();
160+
for (int ans = 1; !q.empty(); ++ans) {
161+
for (int k = q.size(); k; --k) {
162+
auto [i, j] = q.front();
160163
q.pop();
161-
for (int l = 0; l < 4; ++l) {
162-
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
164+
for (int d = 0; d < 4; ++d) {
165+
int x = i + dirs[d], y = j + dirs[d + 1];
163166
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
164-
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
165-
q.push({x, y});
167+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
168+
return ans;
169+
}
166170
maze[x][y] = '+';
171+
q.emplace(x, y);
167172
}
168173
}
169174
}
@@ -178,12 +183,10 @@ public:
178183
```go
179184
func nearestExit(maze [][]byte, entrance []int) int {
180185
m, n := len(maze), len(maze[0])
181-
q := [][]int{entrance}
186+
q := [][2]int{{entrance[0], entrance[1]}}
182187
maze[entrance[0]][entrance[1]] = '+'
183-
ans := 0
184188
dirs := []int{-1, 0, 1, 0, -1}
185-
for len(q) > 0 {
186-
ans++
189+
for ans := 1; len(q) > 0; ans++ {
187190
for k := len(q); k > 0; k-- {
188191
p := q[0]
189192
q = q[1:]
@@ -193,7 +196,7 @@ func nearestExit(maze [][]byte, entrance []int) int {
193196
if x == 0 || x == m-1 || y == 0 || y == n-1 {
194197
return ans
195198
}
196-
q = append(q, []int{x, y})
199+
q = append(q, [2]int{x, y})
197200
maze[x][y] = '+'
198201
}
199202
}
@@ -207,8 +210,6 @@ func nearestExit(maze [][]byte, entrance []int) int {
207210

208211
```ts
209212
function nearestExit(maze: string[][], entrance: number[]): number {
210-
const m = maze.length;
211-
const n = maze[0].length;
212213
const dir = [0, 1, 0, -1, 0];
213214
const q = [[...entrance, 0]];
214215
maze[entrance[0]][entrance[1]] = '+';

solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ class Solution {
22
public:
33
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
44
int m = maze.size(), n = maze[0].size();
5-
queue<vector<int>> q{{entrance}};
5+
int dirs[5] = {-1, 0, 1, 0, -1};
6+
queue<pair<int, int>> q;
7+
q.emplace(entrance[0], entrance[1]);
68
maze[entrance[0]][entrance[1]] = '+';
7-
int ans = 0;
8-
vector<int> dirs = {-1, 0, 1, 0, -1};
9-
while (!q.empty()) {
10-
++ans;
11-
for (int k = q.size(); k > 0; --k) {
12-
auto p = q.front();
9+
for (int ans = 1; !q.empty(); ++ans) {
10+
for (int k = q.size(); k; --k) {
11+
auto [i, j] = q.front();
1312
q.pop();
14-
for (int l = 0; l < 4; ++l) {
15-
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
13+
for (int d = 0; d < 4; ++d) {
14+
int x = i + dirs[d], y = j + dirs[d + 1];
1615
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
17-
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
18-
q.push({x, y});
16+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
17+
return ans;
18+
}
1919
maze[x][y] = '+';
20+
q.emplace(x, y);
2021
}
2122
}
2223
}
2324
}
2425
return -1;
2526
}
26-
};
27+
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
func nearestExit(maze [][]byte, entrance []int) int {
22
m, n := len(maze), len(maze[0])
3-
q := [][]int{entrance}
3+
q := [][2]int{{entrance[0], entrance[1]}}
44
maze[entrance[0]][entrance[1]] = '+'
5-
ans := 0
65
dirs := []int{-1, 0, 1, 0, -1}
7-
for len(q) > 0 {
8-
ans++
6+
for ans := 1; len(q) > 0; ans++ {
97
for k := len(q); k > 0; k-- {
108
p := q[0]
119
q = q[1:]
@@ -15,11 +13,11 @@ func nearestExit(maze [][]byte, entrance []int) int {
1513
if x == 0 || x == m-1 || y == 0 || y == n-1 {
1614
return ans
1715
}
18-
q = append(q, []int{x, y})
16+
q = append(q, [2]int{x, y})
1917
maze[x][y] = '+'
2018
}
2119
}
2220
}
2321
}
2422
return -1
25-
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
class Solution {
22
public int nearestExit(char[][] maze, int[] entrance) {
3-
int m = maze.length;
4-
int n = maze[0].length;
3+
int m = maze.length, n = maze[0].length;
4+
final int[] dirs = {-1, 0, 1, 0, -1};
55
Deque<int[]> q = new ArrayDeque<>();
66
q.offer(entrance);
77
maze[entrance[0]][entrance[1]] = '+';
8-
int ans = 0;
9-
int[] dirs = {-1, 0, 1, 0, -1};
10-
while (!q.isEmpty()) {
11-
++ans;
8+
for (int ans = 1; !q.isEmpty(); ++ans) {
129
for (int k = q.size(); k > 0; --k) {
13-
int[] p = q.poll();
14-
int i = p[0], j = p[1];
15-
for (int l = 0; l < 4; ++l) {
16-
int x = i + dirs[l], y = j + dirs[l + 1];
10+
var p = q.poll();
11+
for (int d = 0; d < 4; ++d) {
12+
int x = p[0] + dirs[d], y = p[1] + dirs[d + 1];
1713
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
1814
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
1915
return ans;
2016
}
21-
q.offer(new int[] {x, y});
2217
maze[x][y] = '+';
18+
q.offer(new int[] {x, y});
2319
}
2420
}
2521
}
2622
}
2723
return -1;
2824
}
29-
}
25+
}

0 commit comments

Comments
 (0)