|
| 1 | +/** |
| 2 | + * 675. Cut Off Trees for Golf Event |
| 3 | + * https://leetcode.com/problems/cut-off-trees-for-golf-event/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are asked to cut off all the trees in a forest for a golf event. The forest is represented |
| 7 | + * as an m x n matrix. In this matrix: |
| 8 | + * - 0 means the cell cannot be walked through. |
| 9 | + * - 1 represents an empty cell that can be walked through. |
| 10 | + * - A number greater than 1 represents a tree in a cell that can be walked through, and this number |
| 11 | + * is the tree's height. |
| 12 | + * |
| 13 | + * In one step, you can walk in any of the four directions: north, east, south, and west. If you are |
| 14 | + * standing in a cell with a tree, you can choose whether to cut it off. |
| 15 | + * |
| 16 | + * You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value |
| 17 | + * at its cell becomes 1 (an empty cell). |
| 18 | + * |
| 19 | + * Starting from the point (0, 0), return the minimum steps you need to walk to cut off all the |
| 20 | + * trees. If you cannot cut off all the trees, return -1. |
| 21 | + * |
| 22 | + * Note: The input is generated such that no two trees have the same height, and there is at least |
| 23 | + * one tree needs to be cut off. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[][]} forest |
| 28 | + * @return {number} |
| 29 | + */ |
| 30 | +const cutOffTree = (forest) => { |
| 31 | + const treeHeights = forest.flat().filter((height) => height > 1).sort((a, b) => a - b); |
| 32 | + let currentPosition = [0, 0]; |
| 33 | + let totalDistance = 0; |
| 34 | + |
| 35 | + while (treeHeights.length) { |
| 36 | + const gridCopy = forest.map((row) => [...row]); |
| 37 | + const result = findDistance(currentPosition, treeHeights.shift(), gridCopy); |
| 38 | + if (result === null) return -1; |
| 39 | + const [nextPosition, distance] = result; |
| 40 | + currentPosition = nextPosition; |
| 41 | + totalDistance += distance; |
| 42 | + } |
| 43 | + return totalDistance; |
| 44 | + |
| 45 | + function findDistance(startPosition, targetHeight, grid) { |
| 46 | + const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]; |
| 47 | + let queue = [startPosition]; |
| 48 | + let distance = 0; |
| 49 | + |
| 50 | + while (queue.length) { |
| 51 | + const nextLevel = []; |
| 52 | + |
| 53 | + for (const [row, col] of queue) { |
| 54 | + if (grid[row][col] === targetHeight) return [[row, col], distance]; |
| 55 | + if (!grid[row][col]) continue; |
| 56 | + |
| 57 | + for (const [deltaRow, deltaCol] of directions) { |
| 58 | + const newRow = row + deltaRow; |
| 59 | + const newCol = col + deltaCol; |
| 60 | + if ( |
| 61 | + newRow >= 0 && newRow < grid.length && newCol >= 0 |
| 62 | + && newCol < grid[0].length && grid[newRow][newCol] |
| 63 | + ) { |
| 64 | + nextLevel.push([newRow, newCol]); |
| 65 | + } |
| 66 | + } |
| 67 | + grid[row][col] = 0; |
| 68 | + } |
| 69 | + distance += 1; |
| 70 | + queue = nextLevel; |
| 71 | + } |
| 72 | + return null; |
| 73 | + } |
| 74 | +}; |
0 commit comments