leetcode675. Cut Off Trees for Golf Event(Hard)

来源:互联网 发布:cf手游刷枪软件免费 编辑:程序博客网 时间:2024/06/05 21:11

题面

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运行结果

这里写图片描述