题目:

Given n * m non-negative integers representing an elevation map 2d where the area of each cell is 1 * 1, compute how much water it is able to trap after raining.

思路:

这道题是前面一道题的一个延伸,http://www.cnblogs.com/AndyJee/p/4821590.html

前面的给出是一维数组,而这里提供的是二维数组,形象地理解,就是提供了一个立体三维得柱形容器,求该容器所能容纳的最大体积。

由于水是往低处流的, 所以对于这一类trapping water问题,我们只用从最外层开始往内接雨水就可以。

首先从矩阵的最外层中找到最小的柱子,可以通过堆来实现,当堆不为空的情况下,每次弹出的都是高度最小的柱子,这时候从该柱子出发,遍历其周边的四个方向(BSF)的柱子,如果某个柱子未到达或超出边界且尚未被访问,则将该柱子加入堆中,如果该柱子的高度比当前柱子高度小,则更新该柱子的高度,同时记录此处所容纳的水,直至堆为空。

代码:

#include<iostream>
#include<vector>
#include<stdlib.h>
#include<queue> using namespace std; struct cell{
int x;
int y;
int h;
cell(int xx,int yy,int hh):x(xx),y(yy),h(hh){};
bool operator<(const cell &c)const{
return h>c.h;
}
}; /*
bool operator<(const cell &a,const cell &b){
return a.h>b.h;
}
*/ int trapRainWater(const vector<vector<int> > &heights){
int m=heights.size();
int n=heights[].size();
vector<vector<int> > visited(m,vector<int>(n,)); int dx[]={,-,,};
int dy[]={,,,-}; priority_queue<cell> pq;
for(int i=;i<n;i++){
pq.push(cell(,i,heights[][i]));
pq.push(cell(m-,i,heights[m-][i]));
visited[][i]=;
visited[m-][i]=;
} for(int i=;i<m;i++){
pq.push(cell(i,,heights[i][]));
pq.push(cell(i,n-,heights[i][n-]));
visited[i][]=;
visited[i][n-]=;
} int ans=;
while(!pq.empty()){
cell c=pq.top();
pq.pop(); for(int i=;i<;i++){
for(int j=;j<;j++){
int nextx=c.x+dx[i];
int nexty=c.y+dy[i];
if(nextx>= && nextx<m && nexty>= && nexty<n && visited[nextx][nexty]==){
visited[nextx][nexty]=;
int h=max(c.h,heights[nextx][nexty]);
pq.push(cell(nextx,nexty,h));
ans+=max(,c.h-heights[nextx][nexty]);
}
}
}
}
return ans;
} int main(){
vector<vector<int> > heights={
{,,,},
{,,,},
{,,,},
{,,,},
{,,,}
}; cout << trapRainWater(heights) <<endl; // ans=14
return ;
}

(算法)Trapping Rain Water II的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  3. [LeetCode] 407. Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  4. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  7. 407. Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  8. [LintCode] Trapping rain water II

    Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1  ...

  9. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

随机推荐

  1. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  2. HTTP 499 状态码 nginx下 499错误[转]

    日志记录中HTTP状态码出现499错误有多种情况,我遇到的一种情况是nginx反代到一个永远打不开的后端,就这样了,日志状态记录是499.发送字节数是0. 老是有用户反映网站系统时好时坏,因为线上的产 ...

  3. 【提权思路】绕过SecureRDP限制远程连接

    工具可以在百度上下载 直接步入正题 配置好的SecureRDP是限制远程登录的用户 原理是判断来访的计算机名是否在白名单中 如果不在,便出现如上图所示 网上也有绕过方法(https://weibo.c ...

  4. Java基础(1)JDK的安装与环境变量配置

    最近在复习Java基础,第一课就是JDK的安装配置以及环境变量的配置,不多废话,直接开始吧 (1)去Oracle官方网站下载JDK 1.8 Java的历史想必大家也清楚,Sun公司开发的一门面向对象的 ...

  5. 【原】不定义Order属性,通过切面类的定义顺序来决定通知执行的先后顺序

    [结论] 在多个切面类的“切入点相同”并且每个切面都“没有定义order属性”的情况下,则切面类(中的通知)的执行顺序与该切面类在<aop:config>元素中“声明的顺序”相关,即先声明 ...

  6. oracle中的术语

    数据库:数据库是实实在在存在的文件,一个数据库文件体系中大致包含(数据文件DBF.控制文件CTL.日志文件LOG) 数据库实例:每个数据库都会有一个数据库实例与之对应,外界环境要通过与数据库实例的交互 ...

  7. PostgreSQL高可用集群方案收集/主从切换/一主多从(待实践)

    对于业内来说,基本都在围绕主从切换的高可用方案: http://www.10tiao.com/html/175/201509/210974337/1.html https://www.jianshu. ...

  8. Python threads synchronization: Locks, RLocks, Semaphores, Conditions, Events and Queues(Forwarding)

    This article describes the Python threading synchronization mechanisms in details. We are going to s ...

  9. JetBrains 系列软件汉化包

    原文地址:https://blog.csdn.net/pingfangx/article/details/78826145 JetBrains 系列软件汉化包 关键字: Android Studio ...

  10. HDU 4497 GCD and LCM (合数分解)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...