题目:

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. [BZOJ2669] [cqoi2012]局部极小值

    [BZOJ2669] [cqoi2012]局部极小值 Description 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所有相邻格子(相邻是指有公共边或公共顶点) ...

  2. 【堆优化Dijkstra+字典序最短路方案】HDU1385-Minimum Transport Cost

    [题目大意] 给出邻接矩阵以及到达各个点需要付出的代价(起点和终点没有代价),求出从给定起点到终点的最短路,并输出字典序最小的方案. [思路] 在堆优化Dijkstra中,用pre记录前驱.如果新方案 ...

  3. [POI2013]Bajtokomputer

    [POI2013]Bajtokomputer 题目大意: 给定一个长度为\(n(n\le10^6)\)的由\(\{-1,0,1\}\)组成的序列,你可以进行\(A_i+=A_{i-1}\)这样的操作, ...

  4. JDK源码学习笔记——Enum枚举使用及原理

    一.为什么使用枚举 什么时候应该使用枚举呢?每当需要一组固定的常量的时候,如一周的天数.一年四季等.或者是在我们编译前就知道其包含的所有值的集合. 利用 public final static 完全可 ...

  5. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  6. Notepad++源代码阅读——窗口元素组织与布局

    1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...

  7. 技能树升级——Chrome Headless模式 - 全栈客栈 - SegmentFault

    技能树升级--Chrome Headless模式 - 全栈客栈 - SegmentFault TNPM

  8. 【XPath Helper:chrome爬虫网页解析工具 Chrome插件】XPath Helper:chrome爬虫网页解析工具 Chrome插件下载_教程_安装 - 开发者插件 - Chrome插件网

    [XPath Helper:chrome爬虫网页解析工具 Chrome插件]XPath Helper:chrome爬虫网页解析工具 Chrome插件下载_教程_安装 - 开发者插件 - Chrome插 ...

  9. 手动为Android 4.x 手机加入�自己的根证书(CA 证书)

    首先看Android 4.x 系统的证书存放位置: AOSP Android系统中CA证书文件的位置在:/ system/etc/security/cacerts/一系列的以数字命名的.0文件 方法一 ...

  10. mt7620 wireless驱动特性意外发现

    前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...