首页 技术 正文
技术 2022年11月12日
0 收藏 326 点赞 3,384 浏览 2386 个字

原题链接在这里: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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903