题目:

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.

分析:

给定一个二维数组,其中的数字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)的更多相关文章

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

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

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

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

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

  9. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  10. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

随机推荐

  1. MD5前端vue加密

    Vue 前端md5加密用户注册时将加密后的密码发送给后端存储当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配.npm: https://www.npmjs.com/package/crypto ...

  2. Docker 安装,常用命令

    安装Docker 官方所有操作系统安装教程:Install Docker Engine on CentOS | Docker Documentation,其中CentOS安装docker引擎的代码: ...

  3. 了解3D世界的黑魔法-纯Java构造一个简单的3D渲染引擎

    简介: 对于非渲染引擎相关工作的开发者来说,可能认为即使构建最简单的3D程序也非常困难,但事实上并非如此,本篇文章将通过简单的200多行的纯 Java代码,去实践正交投影.简单三角形光栅化.z缓冲(深 ...

  4. DataWorks开发ODPS SQL开发生产环境自动补全ProjectName

    简介: DataWorks标准模式下,支持开发环境和生产环境隔离,开发环境和生产环境的数据库表命名有所区别,如果需要在开发环境访问生产环境的数据库表或者跨项目空间访问其他项目空间的表,需要根据proj ...

  5. 高效使用Java构建工具|Maven篇|云效工程师指北

    ​简介:高效使用Java构建工具|Maven篇.众所周知,当前最主流的Java构建工具为Maven/Gradle/Bazel,针对每一个工具,我将分别从日常工作中常见的场景问题切入,例如依赖管理.构建 ...

  6. 基于 Flutter 的 Web 渲染引擎「北海」正式开源!

    简介: 阿里巴巴历时 3 年自研开发的 Web 渲染引擎北海(英文名:Kraken)正式开源,致力打造易扩展,跨平台,高性能的渲染引擎,并已在优酷.大麦.天猫等业务场景中使用. 作者 | 染陌来源 | ...

  7. [BlockChain] 三方互惠是公共区块链得以发展的基石, dApp数字通证的运转需要可持续性玩法

    ------------------------------- 公链 旷工    开发者/用户 ------------------------------- -------------------- ...

  8. dotnet 使用 TaskTupleAwaiter 同时等待多个任务简化代码写法

    在某些业务逻辑下,需要同时等待多个任务执行完成,才能继续往下执行后续逻辑.等待任务执行的逻辑,大部分情况下需要使用到 Task.WhenAll 方法,代码行数不少.另外,在需要获取多个异步任务的返回值 ...

  9. WPF 已知问题 BitmapDecoder.Create 不支持传入 Asynchronous 的文件流

    这是在 GitHub 上有小伙伴报的问题,在 WPF 中,不支持调用 BitmapDecoder.Create 方法,传入的 FileStream 是配置了 FileOptions.Asynchron ...

  10. 第一章 Jenkins安装配置

    Jenkins官网 # 官网: https://www.jenkins.iohttps://www.jenkins.io/zh/ # docker安装: https://www.jenkins.io/ ...