-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.js
40 lines (40 loc) · 1.05 KB
/
Solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @param {character[][]} grid
* @return {number}
*/
var getFood = function (grid) {
const m = grid.length;
const n = grid[0].length;
const dirs = [-1, 0, 1, 0, -1];
const q = [];
for (let i = 0, x = 1; i < m && x == 1; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] == '*') {
q.push([i, j]);
x = 0;
break;
}
}
}
let ans = 0;
while (q.length) {
++ans;
for (let t = q.length; t > 0; --t) {
const [i, j] = q.shift();
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (grid[x][y] == '#') {
return ans;
}
if (grid[x][y] == 'O') {
grid[x][y] = 'X';
q.push([x, y]);
}
}
}
}
}
return -1;
};