原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/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 theobstacle
can't be reached.1
represents theground
can be walked through.The place with number bigger than 1
represents atree
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.
题解:
用minHeap把树连着坐标高度都保存起来. poll出lowest tree, 用BFS算出起点坐标到lowest tree坐标距离加入res中.
Time Complexity: O(m^2 * n^2). m = forest.size(). n = forest.get(0).size(). 最多有m*n棵树, 每个树poll出来后BFS用时O(m*n).
Space: O(m^n). minHeap, que size.
AC Java:
1 class Solution { 2 int [][] dirs = {{0,1},{0,-1},{1,0},{-1,0}}; 3 4 public int cutOffTree(List<List<Integer>> forest) { 5 if(forest == null || forest.size() == 0 || forest.get(0).size() == 0){ 6 return 0; 7 } 8 9 int m = forest.size(); 10 int n = forest.get(0).size(); 11 12 PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a, b) -> a[2] - b[2]); 13 for(int i = 0; i<m; i++){ 14 for(int j = 0; j<n; j++){ 15 if(forest.get(i).get(j) > 1){ // error 16 minHeap.add(new int[]{i, j, forest.get(i).get(j)}); 17 } 18 } 19 } 20 21 int [] start = new int[2]; 22 int res = 0; 23 while(!minHeap.isEmpty()){ 24 int [] lowest = minHeap.poll(); 25 int step = minStep(forest, start, lowest, m, n); 26 if(step < 0){ 27 return -1; 28 } 29 30 res += step; 31 start[0] = lowest[0]; 32 start[1] = lowest[1]; 33 } 34 35 return res; 36 } 37 38 private int minStep(List<List<Integer>> forest, int [] start, int [] lowest, int m, int n){ 39 int step = 0; 40 41 LinkedList<int []> que = new LinkedList<int []>(); 42 boolean [][] used = new boolean[m][n]; 43 44 que.add(start); 45 used[start[0]][start[1]] = true; 46 while(!que.isEmpty()){ 47 int size = que.size(); 48 for(int i = 0; i<size; i++){ 49 int [] cur = que.poll(); 50 if(cur[0] == lowest[0] && cur[1] == lowest[1]){ 51 return step; 52 } 53 54 for(int [] dir : dirs){ 55 int nx = cur[0] + dir[0]; 56 int ny = cur[1] + dir[1]; 57 if(nx<0 || nx>=m || ny<0 || ny>=n || used[nx][ny] || forest.get(nx).get(ny)==0){ 58 continue; 59 } 60 61 que.add(new int[]{nx, ny}); 62 used[nx][ny] = true; 63 } 64 } 65 66 step++; 67 } 68 69 return -1; 70 } 71 }