Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problems: No.1926,1927 #3992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:BFS

我们可以从入口开始,进行广度优先搜索,每次搜索到一个新的空格子,就将其标记为已访问,并将其加入队列,直到找到一个边界上的空格子,返回步数。

具体地,我们定义一个队列 $q$,初始时我们将 $\textit{entrance}$ 加入队列。定义一个变量 $\textit{ans}$ 记录步数,初始为 $1$。然后我们开始进行广度优先搜索,每一轮我们取出队列中的所有元素,遍历这些元素,对于每个元素,我们尝试向四个方向移动,如果移动后的位置是一个空格子,我们将其加入队列,并将其标记为已访问。如果移动后的位置是边界上的空格子,我们返回 $\textit{ans}$。如果队列为空,我们返回 $-1$。这一轮搜索结束后,我们将 $\textit{ans}$ 加一,继续进行下一轮搜索。

遍历结束后,如果我们没有找到边界上的空格子,我们返回 $-1$。

时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是迷宫的行数和列数。

<!-- tabs:start -->

Expand All @@ -91,19 +99,19 @@ 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
for _ in range(len(q)):
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
```

Expand All @@ -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<int[]> 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});
}
}
}
Expand All @@ -148,21 +152,22 @@ class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
queue<vector<int>> q{{entrance}};
int dirs[5] = {-1, 0, 1, 0, -1};
queue<pair<int, int>> q;
q.emplace(entrance[0], entrance[1]);
maze[entrance[0]][entrance[1]] = '+';
int ans = 0;
vector<int> 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);
}
}
}
Expand All @@ -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:]
Expand All @@ -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] = '+'
}
}
Expand All @@ -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]] = '+';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ Thus, the nearest exit is [1,2], which is 2 steps away.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand All @@ -92,19 +100,19 @@ 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
for _ in range(len(q)):
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
```

Expand All @@ -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<int[]> 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});
}
}
}
Expand All @@ -149,21 +153,22 @@ class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
queue<vector<int>> q{{entrance}};
int dirs[5] = {-1, 0, 1, 0, -1};
queue<pair<int, int>> q;
q.emplace(entrance[0], entrance[1]);
maze[entrance[0]][entrance[1]] = '+';
int ans = 0;
vector<int> 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);
}
}
}
Expand All @@ -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:]
Expand All @@ -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] = '+'
}
}
Expand All @@ -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]] = '+';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ class Solution {
public:
int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
int m = maze.size(), n = maze[0].size();
queue<vector<int>> q{{entrance}};
int dirs[5] = {-1, 0, 1, 0, -1};
queue<pair<int, int>> q;
q.emplace(entrance[0], entrance[1]);
maze[entrance[0]][entrance[1]] = '+';
int ans = 0;
vector<int> 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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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:]
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}
}
Loading
Loading