[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-negative 2D map, in this map:
0
represents theobstacle
can't be reached.1
represents theground
can be walked through.The place with number bigger than 1
represents atree
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/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Cut Off Trees for Golf Event 为高尔夫赛事砍树的更多相关文章
- [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 ...
- 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 ...
- LeetCode 675. Cut Off Trees for Golf Event
原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...
- [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 ...
- 675. Cut Off Trees for Golf Event
// Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...
- [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 ...
- Leetcode 675.为高尔夫比赛砍树
为高尔夫比赛砍树 你被请来给一个要举办高尔夫比赛的树林砍树. 树林由一个非负的二维数组表示, 在这个数组中: 0 表示障碍,无法触碰到. 1 表示可以行走的地面. 比1大的数 表示一颗允许走过的树的高 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- Orcle查询优化改写-----单表查询
1.将空值转化为实际值 coalesce 返回第一个不是null的参数 2.查询满足多个条件的行 需要注意,对于多个条件组合,要使用括号,这样在更改维护语句时可以不吸烟再考虑优先级问题,而且可以很容 ...
- Repository个人实践
1.背景 最近,有空了,想着把之前一些乱七八糟的小项目给整理一下,尤其是涉及到Repository.UoW几处.为此,专门查阅了博客园中几个大神 关于Repository的实践,到最后都感觉依然莫衷一 ...
- Django学习(六)---博客文章页面的超链接设置
Django中的超链接 超链接的目标地址 href后面是目标地址 template中可以用 {% url 'app_name : url_name' param %} app_name:应用命名 ...
- Alpha冲刺No.6
站立式会议 继续页面设计 在安卓内构件数据库相应类 解决摄像头.照片的使用的异常问题 二.实际项目进展 页面设计完成百分80 类架构完成 在虚拟机中,能够完成摄像头的调用和程序的使用 三.燃尽图 四. ...
- CPP 栈 示例
#include<iostream> #include<stdlib.h> using namespace std; typedef struct node { int dat ...
- nyoj 公约数和公倍数
公约数和公倍数 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 小明被一个问题给难住了,现在需要你帮帮忙.问题是:给出两个正整数,求出它们的最大公约数和最小公倍数. ...
- 浏览器端类EXCEL表格插件 版本更新 - 智表ZCELL产品V1.1.0.1版本发布
智表(ZCELL),浏览器下纯JS表格控件,为您提供EXCEL般的智能体验! 纯国产化.高性价比的可靠解决方案. 更新说明 让大家久等了.因为最近忙其他项目,发布时间稍有延迟. 下次版本更新 ...
- python全栈开发-常用模块的一些应用
一.random模块详解 1.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 2.常用方法 1. random ...
- 新概念英语(1-127)A famous actoress(女演员)
A:Can you recognize that woman, Liz ?B:I think I can, Kate. It must be Karen Marsh, the actoress.A:I ...
- pandas.DataFrame.describe 官方文档翻译percentile_width,percentiles,include, exclude
使用格式:DataFrame.describe(percentile_width=None, percentiles=None, include=None, exclude=None) 作用:生成 ...