675. Cut Off Trees for Golf Event

xiaoxiao2025-04-16  18

Description

You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:

0 represents the obstacle can’t be reached. 1 represents the ground can be walked through. The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the tree’s height. You are asked to cut off all the trees in this forest in the order of tree’s height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).

You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can’t cut off all the trees, output -1 in that situation.

You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off.

Example 1: Input: [ [1,2,3], [0,0,4], [7,6,5] ] Output: 6 Example 2: Input: [ [1,2,3], [0,0,0], [7,6,5] ] Output: -1 Example 3: Input: [ [2,3,4], [0,0,5], [8,7,6] ] Output: 6 Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking. Hint: size of the given matrix will not exceed 50x50.

Problem URL


Solution

给一个int二维数组,代表了i j 位置的树的高度,0不通,1为平。按照从矮到高的顺序砍树。问需要最少走几步砍完。

Using a priority queue to find which tree we would cut. Using BFS to find the shortest steps. Using int[] store the postion and height of a tree. Add all of them into pq with ascending sorting.

Then preform BFS to find the steps needed to get the next position.

Code

class Solution { private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; //int[][] dirs = {{0,1}, {0, -1}, {1, 0}, {-1, 0}}; public int cutOffTree(List<List<Integer>> forest) { if (forest.size() == 0){ return 0; } int m = forest.size(), n = forest.get(0).size(); PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){ @Override public int compare(int[] a, int[] b){ return a[2] - b[2]; } }); for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ if (forest.get(i).get(j) > 1){ pq.add(new int[]{i, j, forest.get(i).get(j)}); } } } int[] position = new int[2]; int sum = 0; while(!pq.isEmpty()){ int[] tree = pq.poll(); int step = bfs(forest, position, tree, m, n); if (step < 0){ return -1; } sum += step; position[0] = tree[0]; position[1] = tree[1]; } return sum; } private int bfs(List<List<Integer>> forest, int[] position, int[] tree, int m, int n){ int step = 0; boolean[][] visited = new boolean[m][n]; Queue<int[]> queue = new LinkedList<>(); queue.add(position); visited[position[0]][position[1]] = true; while (!queue.isEmpty()){ int size = queue.size(); for (int i = 0; i < size; i++){ int[] current = queue.poll(); if (current[0] == tree[0] && current[1] == tree[1]){ return step; } for (int[] dir : dirs){ int nextRow = current[0] + dir[0]; int nextCol = current[1] + dir[1]; if (nextRow < 0 || nextRow >= m || nextCol < 0 || nextCol >= n || forest.get(nextRow).get(nextCol) == 0 || visited[nextRow][nextCol]){ continue; } queue.add(new int[]{nextRow, nextCol}); visited[nextRow][nextCol] = true; } } step++; } return -1; } }

Time Complexity: O() Space Complexity: O()


Review

转载请注明原文地址: https://www.6miu.com/read-5028429.html

最新回复(0)