forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCut Off Trees for Golf Event.js
38 lines (33 loc) · 1.17 KB
/
Cut Off Trees for Golf Event.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
var cutOffTree = function(forest) {
const trees = forest.flat().filter(x => x && x !== 1).sort((a, b) => b - a);
let currPos = [0, 0], totalDist = 0;
while(trees.length) {
const grid = [...forest.map(row => [...row])];
const res = getDist(currPos, trees.pop(), grid);
if(res == null) return -1;
const [pos, dist] = res;
currPos = pos;
totalDist += dist;
}
return totalDist;
function getDist(start, target, grid) {
const dir = [[1, 0], [-1, 0], [0, 1], [0, -1]];
let queue = [start], dist = 0;
while(queue.length) {
const next = [];
for(let [r, c] of queue) {
if(grid[r][c] === target) return [[r, c], dist];
if(!grid[r][c]) continue;
for(let [x, y] of dir) {
x += r; y += c;
if(x >= 0 && x < grid.length && y >= 0 &&
y < grid[0].length && grid[x][y]) next.push([x, y])
}
grid[r][c] = 0;
}
dist++;
queue = next;
}
return null;
}
};