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. 关于php日期前置是否有0

    例如:2018-01-04,这个日期和月份前置是有0 如果不想有0,date( 'y-n-j',time() ):默认的是date( 'y-m-d',time() ),这个日期和月份前置是有0. da ...

  2. strcat函数

    原型:char  *strcat  ( char  *dest, const  char  *src) 用法:#include  <string.h> 功能:连接两个字符串:strcat( ...

  3. web服务器学习2---httpd-2.4.29虚拟目录及访问控制

    一 创建虚拟目录 环境准备: 系统:CentOS 7.4 软件:httpd-2.4.29 1.编辑主配置文件,添加命令运行子配置文件 vi /usr/local/httpd/conf/httpd.co ...

  4. C和C++运行库

    一.Windows下动态库 1. 静态函数库 这类库的名字一般是libxxx.lib:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后 ...

  5. 网页设计入门<一>

    俗话说:技多不压身.实习周,网页设计是之一,边学边总结... 本次网页设计基于Adobe Dreamweaver CS6开发平台,根据实习老师的暴力指导,为什么说暴力呢? 没有基础,没有预告,打开软件 ...

  6. 修改MYSQL的默认连接时长

    show global variables like 'wait_timeout'; 设置成10小时; set global wait_timeout=36000;

  7. tomcat下的web.xml和项目中的web.xml

    Tomcat 服务器中存在一个web.xml文件 在项目文件夹中同样存在一个web.xml文件 那这两个文件有什么区别呢? tomcat中的web.xml是通用的,如果不设置,那么就会默认是同tomc ...

  8. 使用C#开发Android应用之WebApp

    近段时间了解了一下VS2017开发安卓应用的一些技术,特地把C#开发WebApp的一些过程记录下来, 欢迎大家一起指教.讨论,废话少说,是时候开始表演真正的技术了.. 1.新建空白Android应用 ...

  9. kubernetes进阶(05)kubernetes的命令

    在Kubernetes中,Node.Pod.Replication Controller.Service等概念都可以看作一种资源对象,通过Kubernetes提供的Kubectl工具或者API调用进行 ...

  10. spring-oauth-server实践:使用授权方式四:client_credentials 模式下access_token做业务!!!

    spring-oauth-server入门(1-10)使用授权方式四:client_credentials 模式下access_token做业务!!! 准备工作 授权方式四::客户端方式: 服务网关地 ...