LeetCode 675. Cut Off Trees for Golf Event 为高尔夫比赛砍树 (C++/Java)
题目:
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:
0represents theobstaclecan't be reached.1represents thegroundcan be walked through.The place with number bigger than 1represents atreecan 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.
分析:
给定一个二维数组,其中的数字0表示障碍,1表示平地,大于等于2的数字表示树的高度,现在要求按照树高度由小到大进行砍伐,求最小步数。
先遍历二维数组,将树的高度保存起来并排序,同时记录相应的坐标。
从(0, 0)开始对树依次访问,查找是否有路径到达树,在这里使用BFS进行搜索,如果存在树无法进行访问,意味着我们无法砍伐掉所有的树,返回-1,当遍历完所有的树后,返回统计的步数即可。
程序:
C++
class Solution {
public:
int cutOffTree(vector<vector<int>>& forest) {
m = forest.size();
n = forest[0].size();
vector<tuple<int, int, int>> trees;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; ++j)
if(forest[i][j] > 1)
trees.emplace_back(forest[i][j], i, j);
sort(trees.begin(), trees.end());
int fx = 0;
int fy = 0;
int res = 0;
for(int i = 0; i < trees.size(); i++){
int sx = get<1>(trees[i]);
int sy = get<2>(trees[i]);
int steps = BFS(forest, fx, fy, sx, sy);
if(steps == -1)
return -1;
res += steps;
fx = sx;
fy = sy;
}
return res;
}
private:
int BFS(vector<vector<int>>& forest, int fx, int fy, int sx, int sy){
vector<vector<int>> visited(m, vector<int>(n, 0));
queue<pair<int, int>> q;
static int mov[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
q.emplace(fx, fy);
int steps = 0;
while(!q.empty()){
int num = q.size();
while(num--){
auto node = q.front();
q.pop();
int cx = node.first;
int cy = node.second;
if (cx == sx && cy == sy)
return steps;
for (int i = 0; i < 4; ++i) {
int x = cx + mov[i][0];
int y = cy + mov[i][1];
if (x < 0 || x >= m || y < 0 || y >= n || !forest[x][y]|| visited[x][y])
continue;
visited[x][y] = 1;
q.emplace(x, y);
}
}
steps++;
}
return -1;
}
int m;
int n;
};
Java
class Solution {
public int cutOffTree(List<List<Integer>> forest) {
m = forest.size();
n = forest.get(0).size();
ArrayList<Integer> list = new ArrayList<>();
HashMap<Integer, Pair<Integer, Integer>> map = new HashMap<>();
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
int height = forest.get(i).get(j);
if(height > 1){
list.add(height);
map.put(height, new Pair<>(i, j));
}
}
}
Collections.sort(list);
int fx = 0;
int fy = 0;
int res = 0;
for(Integer i:list){
int sx = map.get(i).getKey();
int sy = map.get(i).getValue();
int steps = BFS(forest, fx, fy, sx, sy);
if(steps == -1)
return -1;
res += steps;
fx = sx;
fy = sy;
}
return res;
}
private int BFS(List<List<Integer>> forest, int fx, int fy, int sx, int sy){
int[][] mov = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int[][] visited = new int[m][n];
ArrayDeque<Pair<Integer, Integer>> arrayDeque = new ArrayDeque<>();
arrayDeque.add(new Pair<>(fx, fy));
int steps = 0;
while(!arrayDeque.isEmpty()){
int num = arrayDeque.size();
while(num-- > 0){
Pair<Integer, Integer> p = arrayDeque.pollFirst();
int cx = p.getKey();
int cy = p.getValue();
if(cx == sx && cy == sy)
return steps;
for (int i = 0; i < 4; ++i) {
int x = cx + mov[i][0];
int y = cy + mov[i][1];
if (x < 0 || x >= m || y < 0 || y >= n || forest.get(x).get(y) == 0 || visited[x][y] == 1)
continue;
visited[x][y] = 1;
arrayDeque.addLast(new Pair<>(x, y));
}
}
steps++;
}
return -1;
}
private int m;
private int n;
}
LeetCode 675. Cut Off Trees for Golf Event 为高尔夫比赛砍树 (C++/Java)的更多相关文章
- [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 ...
- [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 ...
- 675. Cut Off Trees for Golf Event
// Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...
- 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 ...
- [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 ...
- Leetcode 675.为高尔夫比赛砍树
为高尔夫比赛砍树 你被请来给一个要举办高尔夫比赛的树林砍树. 树林由一个非负的二维数组表示, 在这个数组中: 0 表示障碍,无法触碰到. 1 表示可以行走的地面. 比1大的数 表示一颗允许走过的树的高 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
随机推荐
- 学习 XQuery:XML数据查询的关键
XQuery 是 XML 数据的查询语言,类似于 SQL 是数据库的查询语言.它被设计用于查询 XML 数据. XQuery 示例 for $x in doc("books.xml" ...
- Serverless 架构模式及演进
简介: Serverless 架构从使用技术上有计算,数据存储,消息通信,我们可从运维性,安全性,可靠性,可扩展性,成本几个角度来衡量架构的优劣.本文会介绍一些常见的业务场景,探讨如何使用 Serv ...
- 漫画 | 一口气搞懂 Serverless !
简介: 第二届云原生编程挑战赛为热爱技术的年轻人提供一个挑战世界级技术问题的舞台,希望用技术为全社会创造更大价值. 作者 | 刘欣 呃,我可能是别人眼中所说的不用奋斗的一代. 大家喜欢听的什么多姿多 ...
- [GPT] vue 的 quasar 框架 在 layout 模版中 如何获取 子页面当前使用的 useMeta
在 Quasar 框架中,用 Vue Router 的 meta 字段来获取子页面当前使用的 useMeta . 首先,您需要在路由配置中设置子页面的 meta 字段.例如: const rout ...
- FFmpeg开发笔记(十七)Windows环境给FFmpeg集成字幕库libass
libass是一个适用于ASS和SSA格式(Advanced Substation Alpha/Substation Alpha)的字幕渲染器,支持的字幕类型包括srt.ass等,凡是涉及到给视频画 ...
- 快速搭建Zookeeper和Kafka环境
前言 由于项目需要涉及到zookeeper和Kafka的使用,快速做了一篇笔记,方便小伙伴们搭建环境. zookeeper 官方定义 What is ZooKeeper? ZooKeeper is a ...
- ADOBE FORM的一些相关资料
虽然很多人觉得打印程序的开发很无聊(我也这么想),但在实际工作中,打印算是比较有意义的工作,所以还是值得学习的. 之前翻译过几篇Adobe Form的文章,其中的内容,可以帮助创建一些简单的打印示例, ...
- 深入 Django 模型层:数据库设计与 ORM 实践指南
title: 深入 Django 模型层:数据库设计与 ORM 实践指南 date: 2024/5/3 18:25:33 updated: 2024/5/3 18:25:33 categories: ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-44-鼠标操作-上篇
1.简介 前边文章中已经讲解过鼠标的拖拽操作,今天宏哥在这里对其的其他操作进行一个详细地介绍和讲解,然后对其中的一些比较常见的.重要的操作单独拿出来进行详细的介绍和讲解. 2.鼠标操作语法 鼠标操作介 ...
- 用 Certbot-auto 在 letsencrypt.org申请免费 SSL 证书实现 HTTPS
参考帖子 https://www.cnblogs.com/lzpong/p/6433189.html https://www.cnblogs.com/756623607-zhang/p/1163850 ...