59
59
60
60
<!-- 这里可写通用的实现逻辑 -->
61
61
62
- BFS。
62
+ ** 方法一:BFS**
63
+
64
+ 根据题意,我们需要从 ` * ` 出发,找到最近的 ` # ` ,返回最短路径长度。
65
+
66
+ 首先,我们遍历整个二维数组,找到 ` * ` 的位置,将其作为 BFS 的起点,放入队列中。
67
+
68
+ 然后,我们开始 BFS,遍历队列中的元素,每次遍历到一个元素,我们将其上下左右四个方向的元素加入队列中,直到遇到 ` # ` ,返回当前层数。
69
+
70
+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为二维数组的行数和列数。
63
71
64
72
<!-- tabs:start -->
65
73
70
78
``` python
71
79
class Solution :
72
80
def getFood (self , grid : List[List[str ]]) -> int :
73
- def pos ():
74
- for i in range (m):
75
- for j in range (n):
76
- if grid[i][j] == ' *' :
77
- return i, j
78
-
79
81
m, n = len (grid), len (grid[0 ])
80
- q = deque([pos()])
82
+ i, j = next ((i, j) for i in range (m)
83
+ for j in range (n) if grid[i][j] == ' *' )
84
+ q = deque([(i, j)])
85
+ dirs = (- 1 , 0 , 1 , 0 , - 1 )
81
86
ans = 0
82
87
while q:
83
88
ans += 1
84
89
for _ in range (len (q)):
85
90
i, j = q.popleft()
86
- for a, b in [[ 0 , - 1 ], [ 0 , 1 ], [ 1 , 0 ], [ - 1 , 0 ]] :
91
+ for a, b in pairwise(dirs) :
87
92
x, y = i + a, j + b
88
93
if 0 <= x < m and 0 <= y < n:
89
94
if grid[x][y] == ' #' :
@@ -100,20 +105,28 @@ class Solution:
100
105
101
106
``` java
102
107
class Solution {
108
+ private int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
109
+
103
110
public int getFood (char [][] grid ) {
104
- int m = grid. length;
105
- int n = grid[0 ]. length;
106
- Deque<int[]> q = new LinkedList<> ();
107
- q. offer(pos(grid));
111
+ int m = grid. length, n = grid[0 ]. length;
112
+ Deque<int[]> q = new ArrayDeque<> ();
113
+ for (int i = 0 , x = 1 ; i < m && x == 1 ; ++ i) {
114
+ for (int j = 0 ; j < n; ++ j) {
115
+ if (grid[i][j] == ' *' ) {
116
+ q. offer(new int [] {i, j});
117
+ x = 0 ;
118
+ break ;
119
+ }
120
+ }
121
+ }
108
122
int ans = 0 ;
109
- int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
110
123
while (! q. isEmpty()) {
111
124
++ ans;
112
- for (int i = q. size(); i > 0 ; -- i ) {
113
- int [] p = q. poll();
114
- for (int j = 0 ; j < 4 ; ++ j ) {
115
- int x = p[0 ] + dirs[j ];
116
- int y = p[1 ] + dirs[j + 1 ];
125
+ for (int t = q. size(); t > 0 ; -- t ) {
126
+ var p = q. poll();
127
+ for (int k = 0 ; k < 4 ; ++ k ) {
128
+ int x = p[0 ] + dirs[k ];
129
+ int y = p[1 ] + dirs[k + 1 ];
117
130
if (x >= 0 && x < m && y >= 0 && y < n) {
118
131
if (grid[x][y] == ' #' ) {
119
132
return ans;
@@ -128,95 +141,82 @@ class Solution {
128
141
}
129
142
return - 1 ;
130
143
}
131
-
132
- private int [] pos (char [][] grid ) {
133
- for (int i = 0 ; i < grid. length; ++ i) {
134
- for (int j = 0 ; j < grid[0 ]. length; ++ j) {
135
- if (grid[i][j] == ' *' ) {
136
- return new int [] {i, j};
137
- }
138
- }
139
- }
140
- return new int [] {- 1 , - 1 };
141
- }
142
144
}
143
145
```
144
146
145
147
### ** C++**
146
148
147
149
``` cpp
148
- typedef pair<int , int > pii;
149
-
150
150
class Solution {
151
151
public:
152
+ const static inline vector<int > dirs = {-1, 0, 1, 0, -1};
153
+
152
154
int getFood(vector<vector<char>>& grid) {
153
155
int m = grid.size(), n = grid[0].size();
154
- queue<pii > q {{pos(grid)}};
156
+ queue<pair<int, int>> q;
157
+ for (int i = 0, x = 1; i < m && x == 1; ++i) {
158
+ for (int j = 0; j < n; ++j) {
159
+ if (grid[i][j] == '*') {
160
+ q.emplace(i, j);
161
+ x = 0;
162
+ break;
163
+ }
164
+ }
165
+ }
155
166
int ans = 0 ;
156
- vector<int > dirs = {-1, 0, 1, 0, -1};
157
167
while (!q.empty()) {
158
168
++ans;
159
- for (int i = q.size(); i > 0 ; --i ) {
160
- pii p = q.front();
169
+ for (int t = q.size(); t ; --t ) {
170
+ auto [i, j] = q.front();
161
171
q.pop();
162
- for (int j = 0; j < 4; ++j) {
163
- int x = p.first + dirs[ j] ;
164
- int y = p.second + dirs[ j + 1] ;
172
+ for (int k = 0; k < 4; ++k) {
173
+ int x = i + dirs[k], y = j + dirs[k + 1];
165
174
if (x >= 0 && x < m && y >= 0 && y < n) {
166
175
if (grid[x][y] == '#') return ans;
167
176
if (grid[x][y] == 'O') {
168
177
grid[x][y] = 'X';
169
- q.push({ x, y} );
178
+ q.emplace( x, y);
170
179
}
171
180
}
172
181
}
173
182
}
174
183
}
175
184
return -1;
176
185
}
177
-
178
- pii pos(vector<vector<char>>& grid) {
179
- for (int i = 0; i < grid.size(); ++i)
180
- for (int j = 0; j < grid[0].size(); ++j)
181
- if (grid[i][j] == '*')
182
- return {i, j};
183
- return {};
184
- }
185
186
};
186
187
```
187
188
188
189
### ** Go**
189
190
190
191
``` go
191
- func getFood (grid [][]byte ) int {
192
+ func getFood (grid [][]byte ) ( ans int ) {
192
193
m , n := len (grid), len (grid[0 ])
193
- pos := func () []int {
194
- for i := 0 ; i < m; i++ {
195
- for j := 0 ; j < n; j++ {
196
- if grid[i][j] == ' *' {
197
- return []int {i, j}
198
- }
194
+ dirs := []int {-1 , 0 , 1 , 0 , -1 }
195
+ type pair struct { i, j int }
196
+ q := []pair{}
197
+ for i , x := 0 , 1 ; i < m && x == 1 ; i++ {
198
+ for j := 0 ; j < n; j++ {
199
+ if grid[i][j] == ' *' {
200
+ q = append (q, pair{i, j})
201
+ x = 0
202
+ break
199
203
}
200
204
}
201
- return []int {}
202
205
}
203
- q := [][]int {pos ()}
204
- dirs := []int {-1 , 0 , 1 , 0 , -1 }
205
- ans := 0
206
206
for len (q) > 0 {
207
207
ans++
208
- for i := len (q); i > 0 ; i -- {
208
+ for t := len (q); t > 0 ; t -- {
209
209
p := q[0 ]
210
210
q = q[1 :]
211
- for j := 0 ; j < 4 ; j ++ {
212
- x , y := p[ 0 ] +dirs[j ], p[ 1 ] +dirs[j +1 ]
211
+ for k := 0 ; k < 4 ; k ++ {
212
+ x , y := p. i +dirs[k ], p. j +dirs[k +1 ]
213
213
if x >= 0 && x < m && y >= 0 && y < n {
214
214
if grid[x][y] == ' #' {
215
215
return ans
216
216
}
217
217
if grid[x][y] == ' O' {
218
218
grid[x][y] = ' X'
219
- q = append (q, [] int {x, y})
219
+ q = append (q, pair {x, y})
220
220
}
221
221
}
222
222
}
@@ -226,6 +226,51 @@ func getFood(grid [][]byte) int {
226
226
}
227
227
```
228
228
229
+ ### ** JavaScript**
230
+
231
+ ``` js
232
+ /**
233
+ * @param {character[][]} grid
234
+ * @return {number}
235
+ */
236
+ var getFood = function (grid ) {
237
+ const m = grid .length ;
238
+ const n = grid[0 ].length ;
239
+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
240
+ const q = [];
241
+ for (let i = 0 , x = 1 ; i < m && x == 1 ; ++ i) {
242
+ for (let j = 0 ; j < n; ++ j) {
243
+ if (grid[i][j] == ' *' ) {
244
+ q .push ([i, j]);
245
+ x = 0 ;
246
+ break ;
247
+ }
248
+ }
249
+ }
250
+ let ans = 0 ;
251
+ while (q .length ) {
252
+ ++ ans;
253
+ for (let t = q .length ; t > 0 ; -- t) {
254
+ const [i , j ] = q .shift ();
255
+ for (let k = 0 ; k < 4 ; ++ k) {
256
+ const x = i + dirs[k];
257
+ const y = j + dirs[k + 1 ];
258
+ if (x >= 0 && x < m && y >= 0 && y < n) {
259
+ if (grid[x][y] == ' #' ) {
260
+ return ans;
261
+ }
262
+ if (grid[x][y] == ' O' ) {
263
+ grid[x][y] = ' X' ;
264
+ q .push ([x, y]);
265
+ }
266
+ }
267
+ }
268
+ }
269
+ }
270
+ return - 1 ;
271
+ };
272
+ ```
273
+
229
274
### ** ...**
230
275
231
276
```
0 commit comments