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.

Hint: size of the given matrix will not exceed 50x50.

这道题让我们砍掉所有高度大于1的树,而且要求是按顺序从低到高来砍,那么本质实际上还是要求任意两点之间的最短距离啊。对于这种类似迷宫遍历求最短路径的题,BFS是不二之选啊。那么这道题就对高度相邻的两棵树之间调用一个BFS,所以我们可以把BFS的内容放倒子函数helper中来使用。那么我们首先就要将所有的树从低到高进行排序,我们遍历原二位矩阵,将每棵树的高度及其横纵坐标取出来,组成一个三元组,然后放到vector中,之后用sort对数组进行排序,因为sort默认是以第一个数字排序,这也是为啥我们要把高度放在第一个位置。然后我们就遍历我们的trees数组,我们的起始位置是(0,0),终点位置是从trees数组中取出的树的位置,然后调用BFS的helper函数,这个BFS的子函数就是很基本的写法,没啥过多需要讲解的地方,会返回最短路径的值,如果无法到达目标点,就返回-1。所以我们先检查,如果helper函数返回-1了,那么我们就直接返回-1,否则就将cnt加到结果res中。然后更新我们的起始点为当前树的位置,然后循环取下一棵树即可,参见代码如下:

class Solution {
public:
int cutOffTree(vector<vector<int>>& forest) {
int m = forest.size(), n = forest[].size(), res = , row = , col = ;
vector<vector<int>> trees;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (forest[i][j] > ) trees.push_back({forest[i][j], i, j});
}
}
sort(trees.begin(), trees.end());
for (int i = ; i < trees.size(); ++i) {
int cnt = helper(forest, row, col, trees[i][], trees[i][]);
if (cnt == -) return -;
res += cnt;
row = trees[i][];
col = trees[i][];
}
return res;
}
int helper(vector<vector<int>>& forest, int row, int col, int treeRow, int treeCol) {
if (row == treeRow && col == treeCol) return ;
int m = forest.size(), n = forest[].size(), cnt = ;
queue<int> q{{row * n + col}};
vector<vector<int>> visited(m, vector<int>(n));
vector<int> dir{-, , , , -};
while (!q.empty()) {
++cnt;
for (int i = q.size(); i > ; --i) {
int r = q.front() / n, c = q.front() % n; q.pop();
for (int k = ; k < ; ++k) {
int x = r + dir[k], y = c + dir[k + ];
if (x < || x >= m || y < || y >= n || visited[x][y] == || forest[x][y] == ) continue;
if (x == treeRow && y == treeCol) return cnt;
visited[x][y] = ;
q.push(x * n + y);
}
}
}
return -;
}
};

参考资料:

https://leetcode.com/problems/cut-off-trees-for-golf-event/

https://leetcode.com/problems/cut-off-trees-for-golf-event/discuss/107403/c-sort-bfs-with-explanation

https://leetcode.com/problems/cut-off-trees-for-golf-event/discuss/107404/java-solution-priorityqueue-bfs

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  3. LeetCode 675. Cut Off Trees for Golf Event

    原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...

  4. [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 ...

  5. 675. Cut Off Trees for Golf Event

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

  6. [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 ...

  7. Leetcode 675.为高尔夫比赛砍树

    为高尔夫比赛砍树 你被请来给一个要举办高尔夫比赛的树林砍树. 树林由一个非负的二维数组表示, 在这个数组中: 0 表示障碍,无法触碰到. 1 表示可以行走的地面. 比1大的数 表示一颗允许走过的树的高 ...

  8. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  9. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

随机推荐

  1. Sort of Python

    表达式和运算符 什么是表达式? 1+2*3 就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数.1+2*3 经过计算后得到的结果是7,就1+2*3 = 7.我们可以将计算结果保存在一个变 ...

  2. JavaScript 哈希表(散列表)应用

    查找的效率与比较次数密切相关.基于比较的程序,运算效率是比较低的.比如平时可以通过indexOf查找一个数据.但这是一个基于比较的一个实现.如果是淘宝那样有上亿个商品,那么用indeOf 来查数据就会 ...

  3. eclipse如何debug调试jdk源码(任何源码)并显示局部变量

    最近要看struts2源码 仿照了一下查看jdk源码的方式 首先你要有strtus2的jar包和源码,在struts官网上下载时,选择full版本,里面会有src也就是源码了. jar导入项目,保证可 ...

  4. 读论文系列:Object Detection CVPR2016 YOLO

    CVPR2016: You Only Look Once:Unified, Real-Time Object Detection 转载请注明作者:梦里茶 YOLO,You Only Look Once ...

  5. 第二次作业-关于Steam游戏平台的简单分析

    1.1 Steam平台的简单介绍 你选择的产品是? 如题,这次的作业我选择了Steam作为分析的对象. 为什么选择该产品作为分析? 我选择数字游戏贩售平台STEAM作为分析对象的原因有以下几点: 1. ...

  6. C语言第七次作业

    一.PTA实验作业 题目1:求整数序列中出现次数最多的数 1.本题PTA提交列表 2.设计思路 定义一个整型数组a[1001],i,j 为循环变量,N,定义数组b[1001]={0} 输入N for( ...

  7. 开启Linux的share

    1.挂载Install  Vmare Tool 2.解压VMwareTools.tar.gz 3.安装share目录 4.shared Folders Enabled 添加共享的目录. 在自己的Unb ...

  8. redux的知识点

    Redux: Redux 是针对 JavaScript应用的可预测状态容器 就是用来管理数据的.stroe 保存数据action领导 下达命令reducer员工 执行命令 下载命令:  npm ins ...

  9. C# 封装miniblink 使用HTML/CSS/JS来构建.Net 应用程序界面和简易浏览器

    MiniBlink的作者是 龙泉寺扫地僧 miniblink是什么?   (抄了一下 龙泉寺扫地僧 写的简洁) Miniblink是一个全新的.追求极致小巧的浏览器内核项目,其基于chromium最新 ...

  10. 《javascript设计模式与开发实践》阅读笔记(10)—— 组合模式

    组合模式:一些子对象组成一个父对象,子对象本身也可能是由一些孙对象组成. 有点类似树形结构的意思,这里举一个包含命令模式的例子 var list=function(){ //创建接口对象的函数 ret ...