Skip to content

Commit ead50fd

Browse files
committed
feat: add solutions to lc problem: No.1091
No.1091.Shortest Path in Binary Matrix
1 parent 7048792 commit ead50fd

File tree

7 files changed

+188
-97
lines changed

7 files changed

+188
-97
lines changed

solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md

+73-33
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
BFS 最短路模型。
58+
**方法一:BFS**
59+
60+
根据题目描述,一条畅通路径是从左上角单元格 $(0, 0)$ 到右下角单元格 $(n - 1, n - 1)$ 的路径,且路径上所有单元格的值都是 $0$。
61+
62+
因此,如果左上角单元格 $(0, 0)$ 的值为 $1$,则不存在满足要求的路径,直接返回 $-1$。
63+
64+
否则,我们创建一个队列 $q$,将左上角单元格 $(0, 0)$ 加入队列,并且将其标记为已访问,即把 $grid[0][0]$ 的值置为 $1$,然后开始广度优先搜索。
65+
66+
在每一轮搜索中,我们每次取出队首节点 $(i, j)$,如果 $(i, j)$ 为右下角单元格 $(n - 1, n - 1)$,则路径长度为当前的搜索轮数,直接返回。否则,我们将当前节点的所有未被访问过的相邻节点加入队列,并且将它们标记为已访问。每一轮搜索结束后,我们将搜索轮数增加 $1$。然后继续执行上述过程,直到队列为空或者找到目标节点。
67+
68+
如果在搜索结束后,我们仍然没有到达右下角的节点,那么说明右下角的节点不可达,返回 $-1$。
69+
70+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是给定的二进制矩阵的边长。
5971

6072
<!-- tabs:start -->
6173

@@ -69,20 +81,20 @@ class Solution:
6981
if grid[0][0]:
7082
return -1
7183
n = len(grid)
72-
q = deque([(0, 0)])
7384
grid[0][0] = 1
74-
ans = 0
85+
q = deque([(0, 0)])
86+
ans = 1
7587
while q:
76-
ans += 1
7788
for _ in range(len(q)):
7889
i, j = q.popleft()
79-
if (i, j) == (n - 1, n - 1):
90+
if i == j == n - 1:
8091
return ans
8192
for x in range(i - 1, i + 2):
8293
for y in range(j - 1, j + 2):
8394
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
84-
q.append((x, y))
8595
grid[x][y] = 1
96+
q.append((x, y))
97+
ans += 1
8698
return -1
8799
```
88100

@@ -97,23 +109,21 @@ class Solution {
97109
return -1;
98110
}
99111
int n = grid.length;
100-
Deque<int[]> q = new ArrayDeque<>();
101-
q.offer(new int[] {0, 0});
102112
grid[0][0] = 1;
103-
int ans = 0;
104-
while (!q.isEmpty()) {
105-
++ans;
106-
for (int m = q.size(); m > 0; --m) {
107-
int[] p = q.poll();
113+
Deque<int[]> q = new ArrayDeque<>();
114+
q.offer(new int[]{0, 0});
115+
for (int ans = 1; !q.isEmpty(); ++ans) {
116+
for (int k = q.size(); k > 0; --k) {
117+
var p = q.poll();
108118
int i = p[0], j = p[1];
109119
if (i == n - 1 && j == n - 1) {
110120
return ans;
111121
}
112122
for (int x = i - 1; x <= i + 1; ++x) {
113123
for (int y = j - 1; y <= j + 1; ++y) {
114124
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
115-
q.offer(new int[] {x, y});
116125
grid[x][y] = 1;
126+
q.offer(new int[]{x, y});
117127
}
118128
}
119129
}
@@ -130,24 +140,25 @@ class Solution {
130140
class Solution {
131141
public:
132142
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
133-
if (grid[0][0]) return -1;
143+
if (grid[0][0]) {
144+
return -1;
145+
}
134146
int n = grid.size();
135-
queue<pair<int, int>> q;
136-
q.push({0, 0});
137147
grid[0][0] = 1;
138-
int ans = 0;
139-
while (!q.empty()) {
140-
++ans;
141-
for (int m = q.size(); m > 0; --m) {
142-
auto p = q.front();
148+
queue<pair<int, int>> q;
149+
q.emplace(0, 0);
150+
for (int ans = 1; !q.empty(); ++ans) {
151+
for (int k = q.size(); k; --k) {
152+
auto [i, j] = q.front();
143153
q.pop();
144-
int i = p.first, j = p.second;
145-
if (i == n - 1 && j == n - 1) return ans;
154+
if (i == n - 1 && j == n - 1) {
155+
return ans;
156+
}
146157
for (int x = i - 1; x <= i + 1; ++x) {
147158
for (int y = j - 1; y <= j + 1; ++y) {
148-
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
149-
q.push({x, y});
159+
if (x >= 0 && x < n && y >= 0 && y < n && !grid[x][y]) {
150160
grid[x][y] = 1;
161+
q.emplace(x, y);
151162
}
152163
}
153164
}
@@ -166,23 +177,21 @@ func shortestPathBinaryMatrix(grid [][]int) int {
166177
return -1
167178
}
168179
n := len(grid)
169-
q := [][]int{[]int{0, 0}}
170180
grid[0][0] = 1
171-
ans := 0
172-
for len(q) > 0 {
173-
ans++
174-
for m := len(q); m > 0; m-- {
181+
q := [][2]int{{0, 0}}
182+
for ans := 1; len(q) > 0; ans++ {
183+
for k := len(q); k > 0; k-- {
175184
p := q[0]
176-
q = q[1:]
177185
i, j := p[0], p[1]
186+
q = q[1:]
178187
if i == n-1 && j == n-1 {
179188
return ans
180189
}
181190
for x := i - 1; x <= i+1; x++ {
182191
for y := j - 1; y <= j+1; y++ {
183192
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
184-
q = append(q, []int{x, y})
185193
grid[x][y] = 1
194+
q = append(q, [2]int{x, y})
186195
}
187196
}
188197
}
@@ -192,6 +201,37 @@ func shortestPathBinaryMatrix(grid [][]int) int {
192201
}
193202
```
194203

204+
### **TypeScript**
205+
206+
```ts
207+
function shortestPathBinaryMatrix(grid: number[][]): number {
208+
if (grid[0][0]) {
209+
return -1;
210+
}
211+
const n = grid.length;
212+
grid[0][0] = 1;
213+
let q: number[][] = [[0, 0]];
214+
for (let ans = 1; q.length > 0; ++ans) {
215+
const nq: number[][] = [];
216+
for (const [i, j] of q) {
217+
if (i === n - 1 && j === n - 1) {
218+
return ans;
219+
}
220+
for (let x = i - 1; x <= i + 1; ++x) {
221+
for (let y = j - 1; y <= j + 1; ++y) {
222+
if (x >= 0 && x < n && y >= 0 && y < n && !grid[x][y]) {
223+
grid[x][y] = 1;
224+
nq.push([x, y]);
225+
}
226+
}
227+
}
228+
}
229+
q = nq;
230+
}
231+
return -1;
232+
};
233+
```
234+
195235
### **Rust**
196236

197237
```rust

solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md

+60-32
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ class Solution:
6161
if grid[0][0]:
6262
return -1
6363
n = len(grid)
64-
q = deque([(0, 0)])
6564
grid[0][0] = 1
66-
ans = 0
65+
q = deque([(0, 0)])
66+
ans = 1
6767
while q:
68-
ans += 1
6968
for _ in range(len(q)):
7069
i, j = q.popleft()
71-
if (i, j) == (n - 1, n - 1):
70+
if i == j == n - 1:
7271
return ans
7372
for x in range(i - 1, i + 2):
7473
for y in range(j - 1, j + 2):
7574
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
76-
q.append((x, y))
7775
grid[x][y] = 1
76+
q.append((x, y))
77+
ans += 1
7878
return -1
7979
```
8080

@@ -87,23 +87,21 @@ class Solution {
8787
return -1;
8888
}
8989
int n = grid.length;
90-
Deque<int[]> q = new ArrayDeque<>();
91-
q.offer(new int[] {0, 0});
9290
grid[0][0] = 1;
93-
int ans = 0;
94-
while (!q.isEmpty()) {
95-
++ans;
96-
for (int m = q.size(); m > 0; --m) {
97-
int[] p = q.poll();
91+
Deque<int[]> q = new ArrayDeque<>();
92+
q.offer(new int[]{0, 0});
93+
for (int ans = 1; !q.isEmpty(); ++ans) {
94+
for (int k = q.size(); k > 0; --k) {
95+
var p = q.poll();
9896
int i = p[0], j = p[1];
9997
if (i == n - 1 && j == n - 1) {
10098
return ans;
10199
}
102100
for (int x = i - 1; x <= i + 1; ++x) {
103101
for (int y = j - 1; y <= j + 1; ++y) {
104102
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
105-
q.offer(new int[] {x, y});
106103
grid[x][y] = 1;
104+
q.offer(new int[]{x, y});
107105
}
108106
}
109107
}
@@ -120,24 +118,25 @@ class Solution {
120118
class Solution {
121119
public:
122120
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
123-
if (grid[0][0]) return -1;
121+
if (grid[0][0]) {
122+
return -1;
123+
}
124124
int n = grid.size();
125-
queue<pair<int, int>> q;
126-
q.push({0, 0});
127125
grid[0][0] = 1;
128-
int ans = 0;
129-
while (!q.empty()) {
130-
++ans;
131-
for (int m = q.size(); m > 0; --m) {
132-
auto p = q.front();
126+
queue<pair<int, int>> q;
127+
q.emplace(0, 0);
128+
for (int ans = 1; !q.empty(); ++ans) {
129+
for (int k = q.size(); k; --k) {
130+
auto [i, j] = q.front();
133131
q.pop();
134-
int i = p.first, j = p.second;
135-
if (i == n - 1 && j == n - 1) return ans;
132+
if (i == n - 1 && j == n - 1) {
133+
return ans;
134+
}
136135
for (int x = i - 1; x <= i + 1; ++x) {
137136
for (int y = j - 1; y <= j + 1; ++y) {
138-
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
139-
q.push({x, y});
137+
if (x >= 0 && x < n && y >= 0 && y < n && !grid[x][y]) {
140138
grid[x][y] = 1;
139+
q.emplace(x, y);
141140
}
142141
}
143142
}
@@ -156,23 +155,21 @@ func shortestPathBinaryMatrix(grid [][]int) int {
156155
return -1
157156
}
158157
n := len(grid)
159-
q := [][]int{[]int{0, 0}}
160158
grid[0][0] = 1
161-
ans := 0
162-
for len(q) > 0 {
163-
ans++
164-
for m := len(q); m > 0; m-- {
159+
q := [][2]int{{0, 0}}
160+
for ans := 1; len(q) > 0; ans++ {
161+
for k := len(q); k > 0; k-- {
165162
p := q[0]
166-
q = q[1:]
167163
i, j := p[0], p[1]
164+
q = q[1:]
168165
if i == n-1 && j == n-1 {
169166
return ans
170167
}
171168
for x := i - 1; x <= i+1; x++ {
172169
for y := j - 1; y <= j+1; y++ {
173170
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
174-
q = append(q, []int{x, y})
175171
grid[x][y] = 1
172+
q = append(q, [2]int{x, y})
176173
}
177174
}
178175
}
@@ -182,6 +179,37 @@ func shortestPathBinaryMatrix(grid [][]int) int {
182179
}
183180
```
184181

182+
### **TypeScript**
183+
184+
```ts
185+
function shortestPathBinaryMatrix(grid: number[][]): number {
186+
if (grid[0][0]) {
187+
return -1;
188+
}
189+
const n = grid.length;
190+
grid[0][0] = 1;
191+
let q: number[][] = [[0, 0]];
192+
for (let ans = 1; q.length > 0; ++ans) {
193+
const nq: number[][] = [];
194+
for (const [i, j] of q) {
195+
if (i === n - 1 && j === n - 1) {
196+
return ans;
197+
}
198+
for (let x = i - 1; x <= i + 1; ++x) {
199+
for (let y = j - 1; y <= j + 1; ++y) {
200+
if (x >= 0 && x < n && y >= 0 && y < n && !grid[x][y]) {
201+
grid[x][y] = 1;
202+
nq.push([x, y]);
203+
}
204+
}
205+
}
206+
}
207+
q = nq;
208+
}
209+
return -1;
210+
};
211+
```
212+
185213
### **Rust**
186214

187215
```rust

solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
class Solution {
22
public:
33
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
4-
if (grid[0][0]) return -1;
4+
if (grid[0][0]) {
5+
return -1;
6+
}
57
int n = grid.size();
6-
queue<pair<int, int>> q;
7-
q.push({0, 0});
88
grid[0][0] = 1;
9-
int ans = 0;
10-
while (!q.empty()) {
11-
++ans;
12-
for (int m = q.size(); m > 0; --m) {
13-
auto p = q.front();
9+
queue<pair<int, int>> q;
10+
q.emplace(0, 0);
11+
for (int ans = 1; !q.empty(); ++ans) {
12+
for (int k = q.size(); k; --k) {
13+
auto [i, j] = q.front();
1414
q.pop();
15-
int i = p.first, j = p.second;
16-
if (i == n - 1 && j == n - 1) return ans;
15+
if (i == n - 1 && j == n - 1) {
16+
return ans;
17+
}
1718
for (int x = i - 1; x <= i + 1; ++x) {
1819
for (int y = j - 1; y <= j + 1; ++y) {
19-
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
20-
q.push({x, y});
20+
if (x >= 0 && x < n && y >= 0 && y < n && !grid[x][y]) {
2121
grid[x][y] = 1;
22+
q.emplace(x, y);
2223
}
2324
}
2425
}

0 commit comments

Comments
 (0)