题面

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 the obstacle can’t be reached.
1 represents the ground can be walked through.
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.

分析:

这道题目的思路比较简单,就是一道寻找最短路径的问题。根据BFS算法分别求得某个点到某个点的最短路径,再将结果求和就OK。
与传统的BFS不同的地方是,需要对多对点应用BFS,并保存相应的结果,其实也就是一个循环使用BFS的过程。
在做题的过程中,有个小问题,就是,如何记录BFS的步数,以前做题的时候总是简单地将点封装成一个struct,并在其中保存一个步数的成员变量(有点浪费)。而对于这道题目,如果再另外构造这样的节点就有点多此一举了。因此,可以通过对每一个层次的结点数进行计数,从而推断出相应的“步数”。
题目的难度可能更多地体现在循环语句太多导致程序的书写比较复杂……

代码

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>using namespace std;int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};bool validator(vector<vector<int> >& forest, int x, int y) {if (x >= 0 && x < forest.size() && y >= 0 && y < forest[0].size() && forest[x][y] > 0)return true;return false;
}void findSmallLocation(vector<vector<int> >& forest, int& smallestX, int& smallestY) {int min = pow(2, 31) - 1;smallestX = smallestY = 0;for (int i = 0; i < forest.size(); i++) {for (int j = 0; j < forest[0].size(); j++) {if (forest[i][j] > 1 && forest[i][j] < min) {smallestX = i;smallestY = j;min = forest[i][j];}}}
}void resetMap(vector<vector<bool> >& Map) {for (int i = 0; i < Map.size(); i++)for (int j = 0; j < Map[0].size(); j++)Map[i][j] = false;
}int cutOffTree(vector<vector<int> >& forest) {int treeCount = 0; //砍掉所有的树int stepSum = 0;vector<bool> temp;temp.assign(forest[0].size(), false);vector<vector<bool>> Map;Map.assign(forest.size(), temp);for (int i = 0; i < forest.size(); i++) {for (int j = 0; j < forest[0].size(); j++) {if (forest[i][j] > 1)treeCount++;}}int startX = 0;int startY = 0;Map[0][0] = true;int smallestX, smallestY;while (treeCount) {queue<int> locationX, locationY;locationX.push(startX);locationY.push(startY);findSmallLocation(forest, smallestX, smallestY);int cur = 1;int next = 0;int step = 0;while(!locationX.empty()) {int x = locationX.front();int y = locationY.front();if (x == smallestX && y == smallestY) {startX = smallestX;startY = smallestY;forest[x][y] = 1;treeCount--;stepSum += step;resetMap(Map);break;}cur--;for (int i = 0; i < 4; i++) {if (validator(forest, x + dx[i], y + dy[i]) && !Map[x + dx[i]][y + dy[i]]) {Map[x + dx[i]][y + dy[i]] = true;locationX.push(x + dx[i]);locationY.push(y + dy[i]);next++;}}if (cur == 0) {cur = next;step++;next = 0;}locationX.pop();locationY.pop();}if (locationX.empty()) {return -1;}}return stepSum;
}int main() {int x, y;cout << "Please input the x and y: " << endl;cin >> x >> y;vector<int> temp;temp.assign(y, 0);vector<vector<int> > forest;forest.assign(x, temp);for (int i = 0; i < x; i++) {for (int j = 0; j < y; j++) {cin >> forest[i][j];}}cout << cutOffTree(forest) << endl;return 0;
}

各种高度的树也可以通过优先队列来存储,可以减少每次查找最小高度的树的操作。

Leetcode运行结果

leetcode675. Cut Off Trees for Golf Event(Hard)相关推荐

  1. 675. Cut Off Trees for Golf Event

    这道题目最大的难点是理解题意. 文章目录 题目理解 题目理解 输入:一个非负的二维数组 输出:一个最短距离 规则:数组中的元素如果是0,表示障碍,不能通过.如果是1,表示可以行走的地面.如果大于1表示 ...

  2. 一文了解EpiQuik CUT&RUN m6A RNA富集(MeRIP)试剂盒

    EpiQuik CUT&RUN m6A RNA富集(MeRIP)试剂盒用途: EpiQuik™ 切割和运行m6A RNA富集(MeRIP)试剂盒旨在富集RNA含有来自低输入RNA的m6A片段, ...

  3. DICOM:剖析Orthanc中的Web Server,Mongoose之 Flag bit Event(三)

    背景: Orthanc是本专栏中介绍过的一款新型DICOM服务器,具有轻量级.支持REST的特性,可将任意运行Windows和Linux系统的计算机变成DICOM服务器,即miniPACS.Ortha ...

  4. AUTOSAR基础篇之Event(上)

    祥鼠辞旧岁,犇牛踏雪来. 入沪且伊始,便是就地年. 异乡何足惧,同心万里牵. 且待疫散去,幸福中国年. 谨以此打油诗献给诸多跟我一样异地过年的小伙伴们,祝大家在新的一年里能够身体健康,心想事成,牛气冲 ...

  5. Javascript学习------内部对象 String Date event(重要)

    1.String 对象 ·创建String对象:var String =new String(StringText); (事实上任一一个字符串常量都是String对象,可以直接使用: 字符串与Stri ...

  6. JavaScript中的Event(事件)详解

    Event 对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用,函数不会在事件发生前被执行! 事件句柄 (Event ...

  7. Ext JS学习第十六天 事件机制event(一)

    此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件,相信你一定不陌生, 基本事件是什么?就类似于click.keypress.focus. ...

  8. leetcode 617. Merge Two Binary Trees | 617. 合并二叉树(Java)

    题目 https://leetcode-cn.com/problems/merge-two-binary-trees/ 题解 测试用例 输入: [1,3,2,5] [2,1,3,null,4,null ...

  9. input回车触发事件_JavaScript学习笔记(十五)-- Event事件(上)

    EVENT(上) 之前我们简单的了解过一些事件,比如 onclick / onload / onscroll / ... 今天开始,我们详细的学习一些 事件 什么是事件 一个事件由什么东西组成 触发谁 ...

  10. 基础英语语法笔记(全)

    好好学习天天向上的正确表达: Work hard and improve daily! 补充一个和别人对话可以经常使用的固定短语: Believe it or not.信不信由你 注: 词汇量比语法重 ...

最新文章

  1. 刷前端面经笔记(十一)
  2. 阿里云峰会 | AI搜题加速在线教育行业场景创新
  3. Oracle多行函数
  4. java 实现nfa的化简_DFA与NFA的等价性,DFA化简
  5. matlab qtdecomp,Opencv图像识别从零到精通(25)------区域分裂与合并
  6. phpcms调用后台上传的img图片 - 代码篇
  7. 在安卓模拟器中,adb安装apk常见错误
  8. 【kafka】kafka 生态系统 Ecosystem
  9. 1000道Python题库系列分享23(61个填空题)
  10. 如今有线电视还有必要续费吗
  11. 对‘avformat_find_stream_info’未定义的引用、to the PKG_CONFIG_PATH environment variable
  12. Cadence17.2版本原理图绘制
  13. 全球机场三字码,对应的城市三字码
  14. SIM868_GNSS结果解析
  15. intuitionistic fuzzy set 运算规则python实现
  16. ARIMA时间序列分析——(一)数据平稳性检验
  17. 小心肝队-冲刺日志(第六天)
  18. 牛人分析如何高效学习嵌入式
  19. 小强ERP:旅游行业的996,还能改变吗?
  20. MySQL之启动选项

热门文章

  1. 国产光刻机再次斩获新订单,业绩大幅衰退的ASML如遭雷击
  2. Rplot函数图形参数设置
  3. Inflated 3D ConvNet 【I3D】
  4. 1000句最常用英语口语
  5. c4d软件安装上打开不了_Mac安装软件问题,“xxx”已损坏,无法打开
  6. coldfusion_ColdFusion 9有什么新功能?
  7. Zookeeper轻松上手
  8. 接口测试-什么是header头部?
  9. Tiptop CR报表axcr700采购入库月报增加tlf99“多角贸易序号”,部分资料无数据修复4gl文件bug
  10. Todo Tree插件配置