Skip to content

Commit 2a06d42

Browse files
committed
feat: add solutions to lc problem: No.1730
No.1730.Shortest Path to Get Food
1 parent ac77742 commit 2a06d42

File tree

6 files changed

+454
-2
lines changed

6 files changed

+454
-2
lines changed

solution/1700-1799/1730.Shortest Path to Get Food/README.md

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,176 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
BFS。
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

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

7678
```python
77-
79+
class Solution:
80+
def getFood(self, grid: List[List[str]]) -> int:
81+
def pos():
82+
for i in range(m):
83+
for j in range(n):
84+
if grid[i][j] == '*':
85+
return i, j
86+
87+
m, n = len(grid), len(grid[0])
88+
q = deque([pos()])
89+
ans = 0
90+
while q:
91+
ans += 1
92+
for _ in range(len(q), 0, -1):
93+
i, j = q.popleft()
94+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
95+
x, y = i + a, j + b
96+
if 0 <= x < m and 0 <= y < n:
97+
if grid[x][y] == '#':
98+
return ans
99+
if grid[x][y] == 'O':
100+
grid[x][y] = 'X'
101+
q.append((x, y))
102+
return -1
78103
```
79104

80105
### **Java**
81106

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

84109
```java
110+
class Solution {
111+
public int getFood(char[][] grid) {
112+
int m = grid.length;
113+
int n = grid[0].length;
114+
Deque<int[]> q = new LinkedList<>();
115+
q.offer(pos(grid));
116+
int ans = 0;
117+
int[] dirs = {-1, 0, 1, 0, -1};
118+
while (!q.isEmpty()) {
119+
++ans;
120+
for (int i = q.size(); i > 0; --i) {
121+
int[] p = q.poll();
122+
for (int j = 0; j < 4; ++j) {
123+
int x = p[0] + dirs[j];
124+
int y = p[1] + dirs[j + 1];
125+
if (x >= 0 && x < m && y >= 0 && y < n) {
126+
if (grid[x][y] == '#') {
127+
return ans;
128+
}
129+
if (grid[x][y] == 'O') {
130+
grid[x][y] = 'X';
131+
q.offer(new int[]{x, y});
132+
}
133+
}
134+
}
135+
}
136+
}
137+
return -1;
138+
}
139+
140+
private int[] pos(char[][] grid) {
141+
for (int i = 0; i < grid.length; ++i) {
142+
for (int j = 0; j < grid[0].length; ++j) {
143+
if (grid[i][j] == '*') {
144+
return new int[]{i, j};
145+
}
146+
}
147+
}
148+
return new int[]{-1, -1};
149+
}
150+
}
151+
```
152+
153+
### **C++**
154+
155+
```cpp
156+
typedef pair<int, int> pii;
157+
158+
class Solution {
159+
public:
160+
int getFood(vector<vector<char>>& grid) {
161+
int m = grid.size(), n = grid[0].size();
162+
queue<pii> q{{pos(grid)}};
163+
int ans = 0;
164+
vector<int> dirs = {-1, 0, 1, 0, -1};
165+
while (!q.empty())
166+
{
167+
++ans;
168+
for (int i = q.size(); i > 0; --i)
169+
{
170+
pii p = q.front();
171+
q.pop();
172+
for (int j = 0; j < 4; ++j)
173+
{
174+
int x = p.first + dirs[j];
175+
int y = p.second + dirs[j + 1];
176+
if (x >= 0 && x < m && y >= 0 && y < n)
177+
{
178+
if (grid[x][y] == '#') return ans;
179+
if (grid[x][y] == 'O')
180+
{
181+
grid[x][y] = 'X';
182+
q.push({x, y});
183+
}
184+
}
185+
}
186+
}
187+
}
188+
return -1;
189+
}
190+
191+
pii pos(vector<vector<char>>& grid) {
192+
for (int i = 0; i < grid.size(); ++i)
193+
for (int j = 0; j < grid[0].size(); ++j)
194+
if (grid[i][j] == '*')
195+
return {i, j};
196+
return {};
197+
}
198+
};
199+
```
85200

201+
### **Go**
202+
203+
```go
204+
func getFood(grid [][]byte) int {
205+
m, n := len(grid), len(grid[0])
206+
pos := func() []int {
207+
for i := 0; i < m; i++ {
208+
for j := 0; j < n; j++ {
209+
if grid[i][j] == '*' {
210+
return []int{i, j}
211+
}
212+
}
213+
}
214+
return []int{}
215+
}
216+
q := [][]int{pos()}
217+
dirs := []int{-1, 0, 1, 0, -1}
218+
ans := 0
219+
for len(q) > 0 {
220+
ans++
221+
for i := len(q); i > 0; i-- {
222+
p := q[0]
223+
q = q[1:]
224+
for j := 0; j < 4; j++ {
225+
x, y := p[0]+dirs[j], p[1]+dirs[j+1]
226+
if x >= 0 && x < m && y >= 0 && y < n {
227+
if grid[x][y] == '#' {
228+
return ans
229+
}
230+
if grid[x][y] == 'O' {
231+
grid[x][y] = 'X'
232+
q = append(q, []int{x, y})
233+
}
234+
}
235+
}
236+
}
237+
}
238+
return -1
239+
}
86240
```
87241

88242
### **...**

solution/1700-1799/1730.Shortest Path to Get Food/README_EN.md

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,172 @@
7070

7171
## Solutions
7272

73+
BFS.
74+
7375
<!-- tabs:start -->
7476

7577
### **Python3**
7678

7779
```python
78-
80+
class Solution:
81+
def getFood(self, grid: List[List[str]]) -> int:
82+
def pos():
83+
for i in range(m):
84+
for j in range(n):
85+
if grid[i][j] == '*':
86+
return i, j
87+
88+
m, n = len(grid), len(grid[0])
89+
q = deque([pos()])
90+
ans = 0
91+
while q:
92+
ans += 1
93+
for _ in range(len(q), 0, -1):
94+
i, j = q.popleft()
95+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
96+
x, y = i + a, j + b
97+
if 0 <= x < m and 0 <= y < n:
98+
if grid[x][y] == '#':
99+
return ans
100+
if grid[x][y] == 'O':
101+
grid[x][y] = 'X'
102+
q.append((x, y))
103+
return -1
79104
```
80105

81106
### **Java**
82107

83108
```java
109+
class Solution {
110+
public int getFood(char[][] grid) {
111+
int m = grid.length;
112+
int n = grid[0].length;
113+
Deque<int[]> q = new LinkedList<>();
114+
q.offer(pos(grid));
115+
int ans = 0;
116+
int[] dirs = {-1, 0, 1, 0, -1};
117+
while (!q.isEmpty()) {
118+
++ans;
119+
for (int i = q.size(); i > 0; --i) {
120+
int[] p = q.poll();
121+
for (int j = 0; j < 4; ++j) {
122+
int x = p[0] + dirs[j];
123+
int y = p[1] + dirs[j + 1];
124+
if (x >= 0 && x < m && y >= 0 && y < n) {
125+
if (grid[x][y] == '#') {
126+
return ans;
127+
}
128+
if (grid[x][y] == 'O') {
129+
grid[x][y] = 'X';
130+
q.offer(new int[]{x, y});
131+
}
132+
}
133+
}
134+
}
135+
}
136+
return -1;
137+
}
138+
139+
private int[] pos(char[][] grid) {
140+
for (int i = 0; i < grid.length; ++i) {
141+
for (int j = 0; j < grid[0].length; ++j) {
142+
if (grid[i][j] == '*') {
143+
return new int[]{i, j};
144+
}
145+
}
146+
}
147+
return new int[]{-1, -1};
148+
}
149+
}
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
typedef pair<int, int> pii;
156+
157+
class Solution {
158+
public:
159+
int getFood(vector<vector<char>>& grid) {
160+
int m = grid.size(), n = grid[0].size();
161+
queue<pii> q{{pos(grid)}};
162+
int ans = 0;
163+
vector<int> dirs = {-1, 0, 1, 0, -1};
164+
while (!q.empty())
165+
{
166+
++ans;
167+
for (int i = q.size(); i > 0; --i)
168+
{
169+
pii p = q.front();
170+
q.pop();
171+
for (int j = 0; j < 4; ++j)
172+
{
173+
int x = p.first + dirs[j];
174+
int y = p.second + dirs[j + 1];
175+
if (x >= 0 && x < m && y >= 0 && y < n)
176+
{
177+
if (grid[x][y] == '#') return ans;
178+
if (grid[x][y] == 'O')
179+
{
180+
grid[x][y] = 'X';
181+
q.push({x, y});
182+
}
183+
}
184+
}
185+
}
186+
}
187+
return -1;
188+
}
189+
190+
pii pos(vector<vector<char>>& grid) {
191+
for (int i = 0; i < grid.size(); ++i)
192+
for (int j = 0; j < grid[0].size(); ++j)
193+
if (grid[i][j] == '*')
194+
return {i, j};
195+
return {};
196+
}
197+
};
198+
```
84199

200+
### **Go**
201+
202+
```go
203+
func getFood(grid [][]byte) int {
204+
m, n := len(grid), len(grid[0])
205+
pos := func() []int {
206+
for i := 0; i < m; i++ {
207+
for j := 0; j < n; j++ {
208+
if grid[i][j] == '*' {
209+
return []int{i, j}
210+
}
211+
}
212+
}
213+
return []int{}
214+
}
215+
q := [][]int{pos()}
216+
dirs := []int{-1, 0, 1, 0, -1}
217+
ans := 0
218+
for len(q) > 0 {
219+
ans++
220+
for i := len(q); i > 0; i-- {
221+
p := q[0]
222+
q = q[1:]
223+
for j := 0; j < 4; j++ {
224+
x, y := p[0]+dirs[j], p[1]+dirs[j+1]
225+
if x >= 0 && x < m && y >= 0 && y < n {
226+
if grid[x][y] == '#' {
227+
return ans
228+
}
229+
if grid[x][y] == 'O' {
230+
grid[x][y] = 'X'
231+
q = append(q, []int{x, y})
232+
}
233+
}
234+
}
235+
}
236+
}
237+
return -1
238+
}
85239
```
86240

87241
### **...**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
typedef pair<int, int> pii;
2+
3+
class Solution {
4+
public:
5+
int getFood(vector<vector<char>>& grid) {
6+
int m = grid.size(), n = grid[0].size();
7+
queue<pii> q{{pos(grid)}};
8+
int ans = 0;
9+
vector<int> dirs = {-1, 0, 1, 0, -1};
10+
while (!q.empty())
11+
{
12+
++ans;
13+
for (int i = q.size(); i > 0; --i)
14+
{
15+
pii p = q.front();
16+
q.pop();
17+
for (int j = 0; j < 4; ++j)
18+
{
19+
int x = p.first + dirs[j];
20+
int y = p.second + dirs[j + 1];
21+
if (x >= 0 && x < m && y >= 0 && y < n)
22+
{
23+
if (grid[x][y] == '#') return ans;
24+
if (grid[x][y] == 'O')
25+
{
26+
grid[x][y] = 'X';
27+
q.push({x, y});
28+
}
29+
}
30+
}
31+
}
32+
}
33+
return -1;
34+
}
35+
36+
pii pos(vector<vector<char>>& grid) {
37+
for (int i = 0; i < grid.size(); ++i)
38+
for (int j = 0; j < grid[0].size(); ++j)
39+
if (grid[i][j] == '*')
40+
return {i, j};
41+
return {};
42+
}
43+
};

0 commit comments

Comments
 (0)