-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathwallsGates.js
47 lines (39 loc) · 997 Bytes
/
wallsGates.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
const INF = 2147483647;
function wallsAndGates(grid) {
let rows = grid.length;
let cols = grid[0].length;
let queue = [];
for (let r = 0; r < rows; r++) {
for(let c = 0; c < cols; c++) {
if(grid[r][c] === 0){
queue.push([r, c]);
}
}
}
let directions = [[1,0], [-1,0], [0, 1], [0, -1]];
while(queue.length >0) {
let [r, c] = queue.shift();
for(const [dr, dc] of directions) {
const nr = r+dr, nc = c+dc;
if(nr < 0 || nr>= rows || nc < 0 || nc>= cols || grid[nr][nc] !== INF) {
continue;
}
queue.push([nr, nc]);
grid[nr][nc] = grid[r][c]+1;
}
}
}
let grid1 = [
[INF, -1, 0, INF],
[INF, INF, INF, -1],
[INF, -1, INF, -1],
[0, -1, INF, INF]
];
wallsAndGates(grid1)
console.log(grid1);
let grid2 = [
[-1, 0],
[INF, INF]
];
wallsAndGates(grid2)
console.log(grid2);