原题链接在这里: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:

 class Solution {
int [][] dirs = {{0,1},{0,-1},{1,0},{-1,0}}; public int cutOffTree(List<List<Integer>> forest) {
if(forest == null || forest.size() == 0 || forest.get(0).size() == 0){
return 0;
} int m = forest.size();
int n = forest.get(0).size(); PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a, b) -> 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){ // error
minHeap.add(new int[]{i, j, forest.get(i).get(j)});
}
}
} int [] start = new int[2];
int res = 0;
while(!minHeap.isEmpty()){
int [] lowest = minHeap.poll();
int step = minStep(forest, start, lowest, m, n);
if(step < 0){
return -1;
} res += step;
start[0] = lowest[0];
start[1] = lowest[1];
} return res;
} private int minStep(List<List<Integer>> forest, int [] start, int [] lowest, int m, int n){
int step = 0; LinkedList<int []> que = new LinkedList<int []>();
boolean [][] used = new boolean[m][n]; que.add(start);
used[start[0]][start[1]] = true;
while(!que.isEmpty()){
int size = que.size();
for(int i = 0; i<size; i++){
int [] cur = que.poll();
if(cur[0] == lowest[0] && cur[1] == lowest[1]){
return step;
} for(int [] dir : dirs){
int nx = cur[0] + dir[0];
int ny = cur[1] + dir[1];
if(nx<0 || nx>=m || ny<0 || ny>=n || used[nx][ny] || forest.get(nx).get(ny)==0){
continue;
} que.add(new int[]{nx, ny});
used[nx][ny] = true;
}
} step++;
} return -1;
}
}

LeetCode 675. Cut Off Trees for Golf Event的更多相关文章

  1. [LeetCode] 675. 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-nega ...

  2. [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  3. 675. Cut Off Trees for Golf Event

    // Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...

  4. [LeetCode] 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-nega ...

  5. LeetCode - 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-nega ...

  6. [Swift]LeetCode675. 为高尔夫比赛砍树 | 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-nega ...

  7. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. 【Python】深入浅出学习Python的yield和generator

    背景 之前走马观花接触过Python协程的概念,这两天和一个同事聊到了协程,死活想不起来曾经看过的东西,就记得一个yield,概念不清: 所以想捋一捋相关的东西,此篇作为学习的记录. Generato ...

  2. 【三小时学会Kubernetes!(五) 】完成整个架构

    完成整个架构 现在我们学习了完成架构的所有必须的资源,因此这一节会非常快.图 22 中灰色的部分是需要做的事情.让我们从底部开始:部署 sa-logic 的部署. 图 22:当前应用程序状态 部署 S ...

  3. SQL , MERGE 简意

  4. KVM与XEN虚拟化环境究竟有何不同

    虚拟化的概念在近些年收到了很大程度上的普及,求其原因很简单:虚拟化能够最大程度利用资源,为企业节约成本.目前市面较受欢迎的虚拟架构主要有KVM.XEN和VMware,其中,KVM和XEN都是免费开源的 ...

  5. 1004: [HNOI2008]Cards burnside定理

    https://www.lydsy.com/JudgeOnline/problem.php?id=1004 输入数据保证任意多次洗牌都可用这 m种洗牌法中的一种代替,且对每种洗牌法,都存在一种洗牌法使 ...

  6. [java]BoneCP 参数详解

    BoneCP 参数详解: ======================================== 一.BoneCP配置文件格式(bonecp-config.xml):  xml versio ...

  7. 使用VMware出现的各种问题

    ifconfig命令无效 解决办法:yum install net-tools ping不通 cd /etc/sysconfig/network-scripts ls查看所有文件名称,找到ifcfg- ...

  8. IOS-启动图和开屏广告图,类似网易

    作者:若锦 原文链接:http://www.jianshu.com/p/e52806516139 启动图是在iOS开发过程中必不可少的一个部分,很多app在启动图之后会有一张自定义的开屏广告图,点击该 ...

  9. 让nodejs在iis上运行

    node在IIS上运行的好处: Tomasz的回答是我见过最棒的: 使用iisnode模块在IIS中托管node.js应用程序来取代自托管node.exe进程的优势在于: · 进程管理. Iisnod ...

  10. week13《java程序设计》第13次作业总结

    week13<java程序设计>第13次作业总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 答: 1.IP与端口:ip和域名相对应,可找 ...