原题链接在这里: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. max_spare_servers到底是个什么意思?

    pm.max_children=500pm.start_servers=280pm.min_spare_servers=50pm.max_spare_servers=500 把max_spare_se ...

  2. [postgreSql]postgreSql数据库、模式、表、函数的删除与创建

    1.删除/新增数据库    DROP DATABASE "testDB";    CREATE DATABASE "testDB" WITH OWNER = t ...

  3. XAMPP apache443端口被占用

    点击netstat,可以看到443端口被vmvare占用,那只能改端口了, config,选择Apache(http-ssl.conf)文件,找到443端口,改成其他不被占用的端口,就可以了.

  4. 转:在Eclipse的Debug页签中设置虚拟机参数

    http://blog.csdn.net/decorator2015/article/details/50914479 在Eclipse的Debug页签中设置虚拟机参数 步骤 1,Run->De ...

  5. laravel中Crypt加密方法

    使用Crypt::encrypt对数据进行加密,要引入 use Illuminate\Support\Facades\Crypt;;  对使用Crypt::encrypt加密的数据进行解密的方法时:C ...

  6. 019——VUE中v-for与computer结合功能实例讲解

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 转载:【Oracle 集群】RAC知识图文详细教程(四)--缓存融合技术和主要后台进程

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...

  8. fegin 调用源码分析

    http://techblog.ppdai.com/2018/05/28/20180528/ 这篇文章是相当详细

  9. LeetCode OJ:House Robber II(房屋窃贼II)

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  10. 平衡二叉树(AVL)的实现,附可运行C语言代码

    最近几月一直在自学C语言和数据结构,先是写了排序二叉树,觉得平衡二叉树作为一个经典数据结构,有必要实现一下. 网上看了些资料,在AVL和红黑树之间考虑,最后个人还是倾向于AVL. 不同于标准AVL的是 ...