leetcode 407. Trapping Rain Water II BFS广度优先遍历+寻找水+最短木桶现象 + 模拟海水上升

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining.

Note:
Both m and n are less than 110. The height of each unit cell is greater than 0 and is less than 20,000.

Example:

Given the following 3x6 height map:
[
[1,4,3,1,3,2],
[3,2,1,3,2,4],
[2,3,3,2,3,1]
]

Return 4.
这里写图片描述
The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.

这里写图片描述
After the rain, water are trapped between the blocks. The total volume of water trapped is 4.

题意很简单,就是寻找最多的水水量,也就是木板理论,我是参考这个教程 做的,十分棒的做法。

首先我们应该能分析出,能装水的底面肯定不能在边界上,因为边界上的点无法封闭,那么所有边界上的点都可以加入queue,当作BFS的启动点,同时我们需要一个二维数组来标记访问过的点,访问过的点我们用红色来表示,那么如下图所示:

我们再想想,怎么样可以成功的装进去水呢,是不是周围的高度都应该比当前的高度高,形成一个凹槽才能装水,而且装水量取决于周围最小的那个高度,有点像木桶原理的感觉,那么为了模拟这种方法,我们采用模拟海平面上升的方法来做,我们维护一个海平面高度mx,初始化为最小值,从1开始往上升,那么我们BFS遍历的时候就需要从高度最小的格子开始遍历,那么我们的queue就不能使用普通队列了,而是使用优先级队列,将高度小的放在队首,最先取出,这样我们就可以遍历高度为1的三个格子,用绿色标记出来了,如下图所示:

向周围BFS搜索的条件是不能越界,且周围格子未被访问,那么可以看出上面的第一个和最后一个绿格子无法进行进一步搜索,只有第一行中间那个绿格子可以搜索,其周围有一个灰格子未被访问过,将其加入优先队列queue中,然后标记为红色,如下图所示:

那么优先队列queue中高度为1的格子遍历完了,此时海平面上升1,变为2,此时我们遍历优先队列queue中高度为2的格子,有3个,别的就可以了。

强烈建议和leetcode 42. Trapping Rain Water 正反循环遍历求解 一起学习

需要注意的是这里的

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;

这是按照从小到大排列的,less是按照从大到小排列的,也就是优先级队列默认的比较函数是less,这个和数组的比较函数恰恰相反

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>

using namespace std;

class Solution 
{
public:
    int trapRainWater(vector<vector<int>>& heightMap) 
    {
        if (heightMap.size() <= 0)
            return 0;

        //第二个参数设置默认存储结构,默认就是vector,第三个参数是比较函数
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;

        int row = heightMap.size(), col = heightMap[0].size();
        vector<vector<bool>> visit(row,vector<bool>(col,false));
        int sum = 0 , nowHeight = INT_MIN;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (i == 0 || i == row - 1 || j == 0 || j == col-1)
                {
                    que.push(make_pair(heightMap[i][j],i*col+j));
                    visit[i][j] = true;
                }
            }
        }

        vector<vector<int>> dir{ {1,0},{ -1,0 },{ 0,1 },{ 0,-1 } };
        while (que.empty() == false)
        {
            pair<int, int> a = que.top();
            que.pop();
            int height = a.first , x = a.second / col, y = a.second % col;
            nowHeight = max(nowHeight, height);

            for (vector<int> d : dir)
            {
                int x1 = x + d[0], y1 = y + d[1];
                if (x1 >= 0 && x1 < row && y1 >= 0 && y1 < col && visit[x1][y1] == false)
                {
                    que.push(make_pair(heightMap[x1][y1], x1*col + y1));
                    visit[x1][y1] = true;

                    if (heightMap[x1][y1] < nowHeight)
                        sum = sum + nowHeight - heightMap[x1][y1];
                }
            }
        }
        return sum;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值