博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 675. Cut Off Trees for Golf Event
阅读量:7051 次
发布时间:2019-06-28

本文共 2751 字,大约阅读时间需要 9 分钟。

Problem

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 the obstacle can't be reached.

1 represents the ground can be walked through.
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.
Hint: size of the given matrix will not exceed 50x50.

Solution

class Solution {    public int cutOffTree(List
> forest) { List
trees = new ArrayList<>(); for (int i = 0; i < forest.size(); i++) { for (int j = 0; j < forest.get(0).size(); j++) { int value = forest.get(i).get(j); if (value > 1) trees.add(new int[]{i, j, value}); } } Collections.sort(trees, (a,b)->(a[2]-b[2])); int res = 0, i = 0, j = 0; for (int[] tree: trees) { int dist = bfs(forest, i, j, tree[0], tree[1]); if (dist < 0) return -1; else { res += dist; i = tree[0]; j = tree[1]; } } return res; } private int[][] dirs = new int[][]{
{-1,0},{1,0},{0,-1},{0,1}}; private int bfs(List
> forest, int x, int y, int sx, int sy) { int m = forest.size(), n = forest.get(0).size(); Queue
queue = new LinkedList<>(); queue.offer(new int[]{x, y}); boolean[][] visited = new boolean[m][n]; visited[x][y] = true; int dist = -1; while (!queue.isEmpty()) { int size = queue.size(); dist++; for (int i = 0; i < size; i++) { int[] cur = queue.poll(); if (cur[0] == sx && cur[1] == sy) return dist; for (int[] dir: dirs) { int nx = cur[0]+dir[0]; int ny = cur[1]+dir[1]; if (nx < 0 || ny < 0 || nx >= m || ny >= n || visited[nx][ny] || forest.get(nx).get(ny) <= 0) continue; visited[nx][ny] = true; queue.offer(new int[]{nx, ny}); } } } return -1; }}

转载地址:http://mgpol.baihongyu.com/

你可能感兴趣的文章
IOS公司开发者账号申请详细教程
查看>>
ubuntu12.04 安装配置 mysql
查看>>
我的友情链接
查看>>
关于两容器倒水问题的感悟(ACM)
查看>>
Scala Pattern Match之Regular Expressions
查看>>
大型网站技术架构(一)大型网站架构演化
查看>>
Java基础学习总结(16)——Java制作证书的工具keytool用法总结
查看>>
ORACLE 绑定变量用法总结
查看>>
ssh证书登录
查看>>
Swoole学习笔记(五):多协议多端口
查看>>
211学院的小胖子钟情好程序员
查看>>
表格操作
查看>>
用python读写excel文件
查看>>
【Enterprise Manager 12c】如何在EM 12c中配置Exadata Infiniband告警邮件
查看>>
盆盆的11年Microsoft MVP心路历程
查看>>
利用kickstart实现pxe自动安装
查看>>
推荐一个spring的demo网站
查看>>
如何利用互联网工具调研网站
查看>>
最新 Hadoop 视频分享
查看>>
3_Shell语言———输入输出重定向和管道概述
查看>>