题目:

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. 快速获取使用dblink的视图的全部字段

    快速获取使用dblink的视图的全部字段 默认情况下,使用dblink连接的视图在PL/SQL是没法通过代码助手获取全部的字段的 如果要获取全部字段的话,可以使用一个奇怪的方法 对要获取的使用dbli ...

  2. 力扣1076(MySQL)-员工项目Ⅱ(简单)

    题目: 编写一个SQL查询,报告所有雇员最多的项目. 查询结果格式如下所示:  解题思路: 方法一:将两个表联结,以project_id进行分组,统计员工数降序排序,然后筛选出第一条数据. 1 sel ...

  3. Flink 作为现代数据仓库的统一引擎:Hive 集成生产就绪!

    在2020年,你的数据仓库和基础设施需要满足哪些需求? 我们总结了几下几点: 首先,当下的企业正快速转向更实时化的模式,这要求企业具备对线上流式数据进行低延迟处理的能力,以满足实时(real-time ...

  4. 阿里云服务网格ASM集成SLS告警

    ​简介:随着微服务的流行,微服务的架构也在不断的发展演进,Spring Cloud 与 Dubbo为代表的微服务开发框架也得到了普及和落地:在云原生时代,无侵入的服务网格(Service Mesh)开 ...

  5. [DOT] Polkadot-js 的官方资源

    官网:https://polkadot.js.org/ 浏览器扩展(即钱包, 等同以太坊的MetaMask):https://polkadot.js.org/extension/ 钱包的作用方便你管理 ...

  6. Python数据分析 DataFrame 笔记

    08,DataFrame创建 DataFrame是一个[表格型]的数据结构,可以看做是[由Series组成的字典](共用同一个索引).DataFrame由按一定顺序排列的多列数据组成.设计初衷是将Se ...

  7. Codeforces 题解集

    Codeforces Round 940 (Div. 2) and CodeCraft-23 Codeforces Round 940 (Div. 2) and CodeCraft-23 (A-E)

  8. Rust中的并发性:Sync 和 Send Traits

    在并发的世界中,最常见的并发安全问题就是数据竞争,也就是两个线程同时对一个变量进行读写操作.但当你在 Safe Rust 中写出有数据竞争的代码时,编译器会直接拒绝编译.那么它是靠什么魔法做到的呢? ...

  9. 几种常见Ruby on Rails内置方法介绍

    Ruby on Rails是一个功能强大的WEB开发框架,在这里我们将会学到一些经常用到的Ruby on Rails内置方法,帮助大家熟练掌握其应用技巧. Ruby on Rails自动生成文档技巧大 ...

  10. DNS(3) -- dns常用命令-rndc-dig-host-nslookup

    目录 1 bind自带客户端命令 1.1 rndc命令 1.2 检查配置文件语法 2 客户端测试命令 2.1 dig命令 2.2 host命令 2.3 nslookup命令 1 bind自带客户端命令 ...