Skip to content

Commit 774bf76

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

File tree

6 files changed

+353
-23
lines changed

6 files changed

+353
-23
lines changed

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

+125-1
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,146 @@
5555

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

58+
BFS 最短路问题。
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
6163

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

6466
```python
65-
67+
class Solution:
68+
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
69+
if grid[0][0]:
70+
return -1
71+
n = len(grid)
72+
q = deque([(0, 0)])
73+
grid[0][0] = 1
74+
ans = 0
75+
while q:
76+
ans += 1
77+
for _ in range(len(q), 0, -1):
78+
i, j = q.popleft()
79+
if (i, j) == (n - 1, n - 1):
80+
return ans
81+
for x in range(i - 1, i + 2):
82+
for y in range(j - 1, j + 2):
83+
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
84+
q.append((x, y))
85+
grid[x][y] = 1
86+
return -1
6687
```
6788

6889
### **Java**
6990

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

7293
```java
94+
class Solution {
95+
public int shortestPathBinaryMatrix(int[][] grid) {
96+
if (grid[0][0] == 1) {
97+
return -1;
98+
}
99+
int n = grid.length;
100+
Deque<int[]> q = new ArrayDeque<>();
101+
q.offer(new int[]{0, 0});
102+
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();
108+
int i = p[0], j = p[1];
109+
if (i == n - 1 && j == n - 1) {
110+
return ans;
111+
}
112+
for (int x = i - 1; x <= i + 1; ++x) {
113+
for (int y = j - 1; y <= j + 1; ++y) {
114+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
115+
q.offer(new int[]{x, y});
116+
grid[x][y] = 1;
117+
}
118+
}
119+
}
120+
}
121+
}
122+
return -1;
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
133+
if (grid[0][0]) return -1;
134+
int n = grid.size();
135+
queue<pair<int, int>> q;
136+
q.push({0, 0});
137+
grid[0][0] = 1;
138+
int ans = 0;
139+
while (!q.empty())
140+
{
141+
++ans;
142+
for (int m = q.size(); m > 0; --m)
143+
{
144+
auto p = q.front();
145+
q.pop();
146+
int i = p.first, j = p.second;
147+
if (i == n - 1 && j == n - 1) return ans;
148+
for (int x = i - 1; x <= i + 1; ++x)
149+
{
150+
for (int y = j - 1; y <= j + 1; ++y)
151+
{
152+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0)
153+
{
154+
q.push({x, y});
155+
grid[x][y] = 1;
156+
}
157+
}
158+
}
159+
}
160+
}
161+
return -1;
162+
}
163+
};
164+
```
73165
166+
### **Go**
167+
168+
```go
169+
func shortestPathBinaryMatrix(grid [][]int) int {
170+
if grid[0][0] == 1 {
171+
return -1
172+
}
173+
n := len(grid)
174+
q := [][]int{[]int{0, 0}}
175+
grid[0][0] = 1
176+
ans := 0
177+
for len(q) > 0 {
178+
ans++
179+
for m := len(q); m > 0; m-- {
180+
p := q[0]
181+
q = q[1:]
182+
i, j := p[0], p[1]
183+
if i == n-1 && j == n-1 {
184+
return ans
185+
}
186+
for x := i - 1; x <= i+1; x++ {
187+
for y := j - 1; y <= j+1; y++ {
188+
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
189+
q = append(q, []int{x, y})
190+
grid[x][y] = 1
191+
}
192+
}
193+
}
194+
}
195+
}
196+
return -1
197+
}
74198
```
75199

76200
### **...**

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

+125-1
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,142 @@
4949

5050
## Solutions
5151

52+
BFS.
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
```python
57-
59+
class Solution:
60+
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
61+
if grid[0][0]:
62+
return -1
63+
n = len(grid)
64+
q = deque([(0, 0)])
65+
grid[0][0] = 1
66+
ans = 0
67+
while q:
68+
ans += 1
69+
for _ in range(len(q), 0, -1):
70+
i, j = q.popleft()
71+
if (i, j) == (n - 1, n - 1):
72+
return ans
73+
for x in range(i - 1, i + 2):
74+
for y in range(j - 1, j + 2):
75+
if 0 <= x < n and 0 <= y < n and grid[x][y] == 0:
76+
q.append((x, y))
77+
grid[x][y] = 1
78+
return -1
5879
```
5980

6081
### **Java**
6182

6283
```java
84+
class Solution {
85+
public int shortestPathBinaryMatrix(int[][] grid) {
86+
if (grid[0][0] == 1) {
87+
return -1;
88+
}
89+
int n = grid.length;
90+
Deque<int[]> q = new ArrayDeque<>();
91+
q.offer(new int[]{0, 0});
92+
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();
98+
int i = p[0], j = p[1];
99+
if (i == n - 1 && j == n - 1) {
100+
return ans;
101+
}
102+
for (int x = i - 1; x <= i + 1; ++x) {
103+
for (int y = j - 1; y <= j + 1; ++y) {
104+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) {
105+
q.offer(new int[]{x, y});
106+
grid[x][y] = 1;
107+
}
108+
}
109+
}
110+
}
111+
}
112+
return -1;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
123+
if (grid[0][0]) return -1;
124+
int n = grid.size();
125+
queue<pair<int, int>> q;
126+
q.push({0, 0});
127+
grid[0][0] = 1;
128+
int ans = 0;
129+
while (!q.empty())
130+
{
131+
++ans;
132+
for (int m = q.size(); m > 0; --m)
133+
{
134+
auto p = q.front();
135+
q.pop();
136+
int i = p.first, j = p.second;
137+
if (i == n - 1 && j == n - 1) return ans;
138+
for (int x = i - 1; x <= i + 1; ++x)
139+
{
140+
for (int y = j - 1; y <= j + 1; ++y)
141+
{
142+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0)
143+
{
144+
q.push({x, y});
145+
grid[x][y] = 1;
146+
}
147+
}
148+
}
149+
}
150+
}
151+
return -1;
152+
}
153+
};
154+
```
63155
156+
### **Go**
157+
158+
```go
159+
func shortestPathBinaryMatrix(grid [][]int) int {
160+
if grid[0][0] == 1 {
161+
return -1
162+
}
163+
n := len(grid)
164+
q := [][]int{[]int{0, 0}}
165+
grid[0][0] = 1
166+
ans := 0
167+
for len(q) > 0 {
168+
ans++
169+
for m := len(q); m > 0; m-- {
170+
p := q[0]
171+
q = q[1:]
172+
i, j := p[0], p[1]
173+
if i == n-1 && j == n-1 {
174+
return ans
175+
}
176+
for x := i - 1; x <= i+1; x++ {
177+
for y := j - 1; y <= j+1; y++ {
178+
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
179+
q = append(q, []int{x, y})
180+
grid[x][y] = 1
181+
}
182+
}
183+
}
184+
}
185+
}
186+
return -1
187+
}
64188
```
65189

66190
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
4+
if (grid[0][0]) return -1;
5+
int n = grid.size();
6+
queue<pair<int, int>> q;
7+
q.push({0, 0});
8+
grid[0][0] = 1;
9+
int ans = 0;
10+
while (!q.empty())
11+
{
12+
++ans;
13+
for (int m = q.size(); m > 0; --m)
14+
{
15+
auto p = q.front();
16+
q.pop();
17+
int i = p.first, j = p.second;
18+
if (i == n - 1 && j == n - 1) return ans;
19+
for (int x = i - 1; x <= i + 1; ++x)
20+
{
21+
for (int y = j - 1; y <= j + 1; ++y)
22+
{
23+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0)
24+
{
25+
q.push({x, y});
26+
grid[x][y] = 1;
27+
}
28+
}
29+
}
30+
}
31+
}
32+
return -1;
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func shortestPathBinaryMatrix(grid [][]int) int {
2+
if grid[0][0] == 1 {
3+
return -1
4+
}
5+
n := len(grid)
6+
q := [][]int{[]int{0, 0}}
7+
grid[0][0] = 1
8+
ans := 0
9+
for len(q) > 0 {
10+
ans++
11+
for m := len(q); m > 0; m-- {
12+
p := q[0]
13+
q = q[1:]
14+
i, j := p[0], p[1]
15+
if i == n-1 && j == n-1 {
16+
return ans
17+
}
18+
for x := i - 1; x <= i+1; x++ {
19+
for y := j - 1; y <= j+1; y++ {
20+
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0 {
21+
q = append(q, []int{x, y})
22+
grid[x][y] = 1
23+
}
24+
}
25+
}
26+
}
27+
}
28+
return -1
29+
}

0 commit comments

Comments
 (0)