-
Notifications
You must be signed in to change notification settings - Fork 0
/
1162.地图分析.js
59 lines (52 loc) · 1.04 KB
/
1162.地图分析.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode.cn id=1162 lang=javascript
*
* [1162] 地图分析
*/
// @lc code=start
/**
* @param {number[][]} grid
* @return {number}
*/
var maxDistance = function (grid) {
let rows = grid.length,
cols = grid[0].length,
q = [],
node = null,
dx = [0, 0, 1, -1],
dy = [1, -1, 0, 0],
hasOcean = false;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === 1) {
q.push({ x: i, y: j });
}
}
}
while (q.length !== 0) {
node = q.shift();
let x = node.x,
y = node.y;
for (let i = 0; i < 4; i++) {
let newX = x + dx[i];
let newY = y + dy[i];
if (
newX < 0 ||
newX >= rows ||
newY < 0 ||
newY >= cols ||
grid[newX][newY] !== 0
) {
continue;
}
grid[newX][newY] = grid[x][y] + 1;
hasOcean = true;
q.push({ x: newX, y: newY });
}
}
if (node == null || !hasOcean) {
return -1;
}
return grid[node.x][node.y] - 1;
};
// @lc code=end