Skip to content

Commit e567433

Browse files
committed
feat: add solutions to lc problem: No.1926
No.1926.Nearest Exit from Entrance in Maze
1 parent 774bf76 commit e567433

File tree

6 files changed

+331
-2
lines changed

6 files changed

+331
-2
lines changed

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

+114-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,135 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
BFS 最短路问题。
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
<!-- 这里可写当前语言的特殊实现逻辑 -->
7173

7274
```python
73-
75+
class Solution:
76+
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
77+
m, n = len(maze), len(maze[0])
78+
i, j = entrance
79+
q = deque([(i, j)])
80+
maze[i][j] = '+'
81+
ans = 0
82+
while q:
83+
ans += 1
84+
for _ in range(len(q), 0, -1):
85+
i, j = q.popleft()
86+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
87+
x, y = i + a, j + b
88+
if 0 <= x < m and 0 <= y < n and maze[x][y] == '.':
89+
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
90+
return ans
91+
q.append((x, y))
92+
maze[x][y] = '+'
93+
return -1
7494
```
7595

7696
### **Java**
7797

7898
<!-- 这里可写当前语言的特殊实现逻辑 -->
7999

80100
```java
101+
class Solution {
102+
public int nearestExit(char[][] maze, int[] entrance) {
103+
int m = maze.length;
104+
int n = maze[0].length;
105+
Deque<int[]> q = new ArrayDeque<>();
106+
q.offer(entrance);
107+
maze[entrance[0]][entrance[1]] = '+';
108+
int ans = 0;
109+
int[] dirs = {-1, 0, 1, 0, -1};
110+
while (!q.isEmpty()) {
111+
++ans;
112+
for (int k = q.size(); k > 0; --k) {
113+
int[] p = q.poll();
114+
int i = p[0], j = p[1];
115+
for (int l = 0; l < 4; ++l) {
116+
int x = i + dirs[l], y = j + dirs[l + 1];
117+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
118+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
119+
return ans;
120+
}
121+
q.offer(new int[]{x, y});
122+
maze[x][y] = '+';
123+
}
124+
}
125+
}
126+
}
127+
return -1;
128+
}
129+
}
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
138+
int m = maze.size(), n = maze[0].size();
139+
queue<vector<int>> q{{entrance}};
140+
maze[entrance[0]][entrance[1]] = '+';
141+
int ans = 0;
142+
vector<int> dirs = {-1, 0, 1, 0, -1};
143+
while (!q.empty())
144+
{
145+
++ans;
146+
for (int k = q.size(); k > 0; --k)
147+
{
148+
auto p = q.front();
149+
q.pop();
150+
for (int l = 0; l < 4; ++l)
151+
{
152+
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
153+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.')
154+
{
155+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
156+
q.push({x, y});
157+
maze[x][y] = '+';
158+
}
159+
}
160+
}
161+
}
162+
return -1;
163+
}
164+
};
165+
```
81166
167+
### **Go**
168+
169+
```go
170+
func nearestExit(maze [][]byte, entrance []int) int {
171+
m, n := len(maze), len(maze[0])
172+
q := [][]int{entrance}
173+
maze[entrance[0]][entrance[1]] = '+'
174+
ans := 0
175+
dirs := []int{-1, 0, 1, 0, -1}
176+
for len(q) > 0 {
177+
ans++
178+
for k := len(q); k > 0; k-- {
179+
p := q[0]
180+
q = q[1:]
181+
for l := 0; l < 4; l++ {
182+
x, y := p[0]+dirs[l], p[1]+dirs[l+1]
183+
if x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' {
184+
if x == 0 || x == m-1 || y == 0 || y == n-1 {
185+
return ans
186+
}
187+
q = append(q, []int{x, y})
188+
maze[x][y] = '+'
189+
}
190+
}
191+
}
192+
}
193+
return -1
194+
}
82195
```
83196

84197
### **...**

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

+114-1
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,131 @@ Thus, the nearest exit is [1,2], which is 2 steps away.
6060

6161
## Solutions
6262

63+
BFS.
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
```python
68-
70+
class Solution:
71+
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
72+
m, n = len(maze), len(maze[0])
73+
i, j = entrance
74+
q = deque([(i, j)])
75+
maze[i][j] = '+'
76+
ans = 0
77+
while q:
78+
ans += 1
79+
for _ in range(len(q), 0, -1):
80+
i, j = q.popleft()
81+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
82+
x, y = i + a, j + b
83+
if 0 <= x < m and 0 <= y < n and maze[x][y] == '.':
84+
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
85+
return ans
86+
q.append((x, y))
87+
maze[x][y] = '+'
88+
return -1
6989
```
7090

7191
### **Java**
7292

7393
```java
94+
class Solution {
95+
public int nearestExit(char[][] maze, int[] entrance) {
96+
int m = maze.length;
97+
int n = maze[0].length;
98+
Deque<int[]> q = new ArrayDeque<>();
99+
q.offer(entrance);
100+
maze[entrance[0]][entrance[1]] = '+';
101+
int ans = 0;
102+
int[] dirs = {-1, 0, 1, 0, -1};
103+
while (!q.isEmpty()) {
104+
++ans;
105+
for (int k = q.size(); k > 0; --k) {
106+
int[] p = q.poll();
107+
int i = p[0], j = p[1];
108+
for (int l = 0; l < 4; ++l) {
109+
int x = i + dirs[l], y = j + dirs[l + 1];
110+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
111+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
112+
return ans;
113+
}
114+
q.offer(new int[]{x, y});
115+
maze[x][y] = '+';
116+
}
117+
}
118+
}
119+
}
120+
return -1;
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
131+
int m = maze.size(), n = maze[0].size();
132+
queue<vector<int>> q{{entrance}};
133+
maze[entrance[0]][entrance[1]] = '+';
134+
int ans = 0;
135+
vector<int> dirs = {-1, 0, 1, 0, -1};
136+
while (!q.empty())
137+
{
138+
++ans;
139+
for (int k = q.size(); k > 0; --k)
140+
{
141+
auto p = q.front();
142+
q.pop();
143+
for (int l = 0; l < 4; ++l)
144+
{
145+
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
146+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.')
147+
{
148+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
149+
q.push({x, y});
150+
maze[x][y] = '+';
151+
}
152+
}
153+
}
154+
}
155+
return -1;
156+
}
157+
};
158+
```
74159
160+
### **Go**
161+
162+
```go
163+
func nearestExit(maze [][]byte, entrance []int) int {
164+
m, n := len(maze), len(maze[0])
165+
q := [][]int{entrance}
166+
maze[entrance[0]][entrance[1]] = '+'
167+
ans := 0
168+
dirs := []int{-1, 0, 1, 0, -1}
169+
for len(q) > 0 {
170+
ans++
171+
for k := len(q); k > 0; k-- {
172+
p := q[0]
173+
q = q[1:]
174+
for l := 0; l < 4; l++ {
175+
x, y := p[0]+dirs[l], p[1]+dirs[l+1]
176+
if x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' {
177+
if x == 0 || x == m-1 || y == 0 || y == n-1 {
178+
return ans
179+
}
180+
q = append(q, []int{x, y})
181+
maze[x][y] = '+'
182+
}
183+
}
184+
}
185+
}
186+
return -1
187+
}
75188
```
76189

77190
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
4+
int m = maze.size(), n = maze[0].size();
5+
queue<vector<int>> q{{entrance}};
6+
maze[entrance[0]][entrance[1]] = '+';
7+
int ans = 0;
8+
vector<int> dirs = {-1, 0, 1, 0, -1};
9+
while (!q.empty())
10+
{
11+
++ans;
12+
for (int k = q.size(); k > 0; --k)
13+
{
14+
auto p = q.front();
15+
q.pop();
16+
for (int l = 0; l < 4; ++l)
17+
{
18+
int x = p[0] + dirs[l], y = p[1] + dirs[l + 1];
19+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.')
20+
{
21+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans;
22+
q.push({x, y});
23+
maze[x][y] = '+';
24+
}
25+
}
26+
}
27+
}
28+
return -1;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func nearestExit(maze [][]byte, entrance []int) int {
2+
m, n := len(maze), len(maze[0])
3+
q := [][]int{entrance}
4+
maze[entrance[0]][entrance[1]] = '+'
5+
ans := 0
6+
dirs := []int{-1, 0, 1, 0, -1}
7+
for len(q) > 0 {
8+
ans++
9+
for k := len(q); k > 0; k-- {
10+
p := q[0]
11+
q = q[1:]
12+
for l := 0; l < 4; l++ {
13+
x, y := p[0]+dirs[l], p[1]+dirs[l+1]
14+
if x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' {
15+
if x == 0 || x == m-1 || y == 0 || y == n-1 {
16+
return ans
17+
}
18+
q = append(q, []int{x, y})
19+
maze[x][y] = '+'
20+
}
21+
}
22+
}
23+
}
24+
return -1
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int nearestExit(char[][] maze, int[] entrance) {
3+
int m = maze.length;
4+
int n = maze[0].length;
5+
Deque<int[]> q = new ArrayDeque<>();
6+
q.offer(entrance);
7+
maze[entrance[0]][entrance[1]] = '+';
8+
int ans = 0;
9+
int[] dirs = {-1, 0, 1, 0, -1};
10+
while (!q.isEmpty()) {
11+
++ans;
12+
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];
17+
if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') {
18+
if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
19+
return ans;
20+
}
21+
q.offer(new int[]{x, y});
22+
maze[x][y] = '+';
23+
}
24+
}
25+
}
26+
}
27+
return -1;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
3+
m, n = len(maze), len(maze[0])
4+
i, j = entrance
5+
q = deque([(i, j)])
6+
maze[i][j] = '+'
7+
ans = 0
8+
while q:
9+
ans += 1
10+
for _ in range(len(q), 0, -1):
11+
i, j = q.popleft()
12+
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
13+
x, y = i + a, j + b
14+
if 0 <= x < m and 0 <= y < n and maze[x][y] == '.':
15+
if x == 0 or x == m - 1 or y == 0 or y == n - 1:
16+
return ans
17+
q.append((x, y))
18+
maze[x][y] = '+'
19+
return -1

0 commit comments

Comments
 (0)