LeetCode 675. Cut Off Trees for Golf Event

原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/

题目:

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:

  1. 0 represents the obstacle can't be reached.
  2. 1 represents the ground can be walked through.
  3. 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.

题解:

用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 }
复制代码
posted @   Dylan_Java_NYC  阅读(624)  评论(0)    收藏  举报
编辑推荐:
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
阅读排行:
· 一周 Star 破万的开源项目「GitHub 热点速览」
· 编码之道,道心破碎。
· 千万级大表,如何做性能调优?
· 不写代码,让 AI 生成手机 APP!保姆级教程
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方
历史上的今天:
2017-01-30 LeetCode 367. Valid Perfect Square
点击右上角即可分享
微信分享提示