(算法)Trapping Rain Water II
题目:
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的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
随机推荐
- Shell 学习笔记之条件语句
条件语句 if # if if condition then command fi # if else if condition then command else command fi # if e ...
- python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名
import MySQLdb #connect try: conn = MySQLdb.connect( host = "localhost", user = "root ...
- HDU 1754 I Hate It 线段树RMQ
I Hate It Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=175 ...
- 彻底解决每次打开visio都提示windows正在配置visio的问题
出现这个提示windows正在配置XXX软件的问题,是由于在安装一个新的版本时,之前那个版本的office没有完全卸载,注册表内有残留. 最简单的方式,并不是 把HKEY_CURRENT_USER\S ...
- SQLiteSpy - A fast and compact GUI database manager for SQLite
http://www.yunqa.de/delphi/doku.php/products/sqlitespy/index SQLiteSpy is a fast and compact GUI dat ...
- Effective JavaScript Item 35 使用闭包来保存私有数据
本系列作为EffectiveJavaScript的读书笔记. JavaScript的对象系统从其语法上而言并不鼓舞使用信息隐藏(Information Hiding).由于当使用诸如this.name ...
- mysql---总体备份和增量备份
总体备份: 对整张表或者整个数据库甚至全部数据库进行备份. 增量备份: 对某一范围内的数据进行备份. 1.总体备份: 对表进行备份: 针对存储引擎为myisam的表,能够直接复制frm.myd.myi ...
- java内存模型知识点汇总
1.像windows/linux这种操作系统中,自带jvm么?以方便java程序的运行? 答:是的,一般操作系统都自带jvm的.但不带jdk,也就是说java的运行环境有,但编译环境没有. 1.jav ...
- 在IOS 模拟器中 输入中文
模拟器默认的配置种没有“小地球”,只能输入英文.加入中文方法如下: 找到模拟器的Settings--->General-->Keyboard-->International KeyB ...
- Android开发FAQ集锦!!!
.Android SDK应该从什么地方下载?为什么(http://developer.Android.com/ )经常上不上去? 答:谷歌官网的 (http://developer.Android.c ...