leetcode 675. Cut Off Trees for Golf Event 广度优先遍历BFS

来源:互联网 发布:ai软件下载中文版 mac 编辑:程序博客网 时间:2024/06/05 16:04

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.

本题题意很简单,是这样的,有一篇树林,0表示障碍,1表示可以越过,大于1的表示树的高度,现在要去按照树的高度砍伐所有的树,从(0,0)出发求需要的最小的step,典型的BFS遍历做法,这道题很值得学习和反思

代码如下:

#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;class Solution{public:    int cutOffTree(vector<vector<int>>& forest)     {        if (forest.size() <= 0)            return 0;        vector<vector<int>> all;        for (int i = 0; i < forest.size(); i++)        {            for (int j = 0; j < forest[0].size(); j++)            {                if (forest[i][j] > 1)                    all.push_back({forest[i][j],i,j});            }        }        sort(all.begin(), all.end());        int res = 0;        for (int i = 0, x = 0, y = 0; i < all.size(); i++)        {            int step = bfs(forest, x, y, all[i][1], all[i][2]);            if (step == -1)                return -1;            else                res += step;            x = all[i][1];            y = all[i][2];        }        return res;    }    int bfs(vector<vector<int>>& a, int beginX, int beginY, int endX, int endY)    {        if (beginX == endX && beginY == endY)            return 0;        int row = a.size(), col = a[0].size();        vector<vector<bool>> visit(row, vector<bool>(col, false));        queue<pair<int, int>> que;        vector<vector<int>> dir{ {-1,0},{ 1,0 },{ 0,-1 },{ 0,1 }};        visit[beginX][beginY] = true;        que.push({ beginX,beginY });        int step = 0;        while (que.empty() == false)        {            step++;            int size = que.size();            for (int i = 0; i < size; i++)            {                int x = que.front().first, y = que.front().second;                que.pop();                for (vector<int> d : dir)                {                    int tmpx = x + d[0], tmpy = y + d[1];                    if (tmpx < 0 || tmpx >= row || tmpy < 0 || tmpy >= col || visit[tmpx][tmpy] == true || a[tmpx][tmpy] == 0)                        continue;                    if (tmpx == endX && tmpy == endY)                        return step;                    else                    {                        visit[tmpx][tmpy] = true;                        que.push({ tmpx,tmpy });                    }                }            }        }        return -1;    }};
原创粉丝点击