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

Example

Given 5*4 matrix

[12,13,0,12]
[13,4,13,12]
[13,8,10,12]
[12,13,12,12]
[13,13,13,13]

return 14.

struct MyPoint{
int x, y, h;
MyPoint(int xx, int yy, int hh):x(xx), y(yy), h(hh){}
}; struct Cmp{
bool operator()(const MyPoint &p1, const MyPoint &p2){
return p1.h > p2.h;
}
}; class Solution {
public:
/**
* @param heights: a matrix of integers
* @return: an integer
*/
int trapRainWater(vector<vector<int> > &heights) {
int m = heights.size();
if(m < ) return ;
int n = heights[].size();
if(n < ) return ; int waterNum = ;
vector<vector<bool> > visit(m, vector<bool>(n, false));
priority_queue<MyPoint, vector<MyPoint>, Cmp> MyPointQue; for(int i = ;i < n;++i){
MyPointQue.push(MyPoint(, i, heights[][i]));
MyPointQue.push(MyPoint(m - , i, heights[m - ][i]));
visit[][i] = true;
visit[m - ][i] = true;
} for(int j = ;j < m - ;++j){
MyPointQue.push(MyPoint(j, , heights[j][]));
MyPointQue.push(MyPoint(j, n - , heights[j][n - ]));
visit[j][] = true;
visit[j][n - ] = true;
} const int detX[] = {, , , -}, detY[] = {, -, , };
while(MyPointQue.size() > ){
MyPoint p = MyPointQue.top();
MyPointQue.pop(); for(int i = ;i < ;++i){
int xx = p.x + detX[i], yy = p.y + detY[i];
if(xx < || xx >= m || yy < || yy >= n) continue;
if(visit[xx][yy]) continue;
else{
int h = heights[xx][yy];
if(heights[xx][yy] < p.h){
h = p.h;
waterNum += p.h - heights[xx][yy];
}
MyPointQue.push(MyPoint(xx, yy, h));
visit[xx][yy] = true;
}
}
}
return waterNum;
}
};

[LintCode] 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. [LintCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【Lintcode】364.Trapping Rain Water II

    题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell i ...

随机推荐

  1. 第10章 使用Apache服务部署静态网站

    章节简述: 本章节中通过对比目前热门的网站服务程序来说明Apache服务程序的优势,并新增主机空间选购技巧小节. 了解SELinux服务的3种工作模式,小心谨慎的使用semanage命令和setseb ...

  2. 教你安装漂亮的Arc GTK主题

    导读 近日,我们又发现了一款深受 Linux 用户喜爱的桌面主题 — Arc GTK,Arc GTK 主题已被很多 GNU/Linux 操作系统支持和采用,其中就包括即将到来的 Linux Mint ...

  3. Struts2配置文件模板

    <?xml version = "1.0" encoding = "UTF-8"?><!--下面是Struts2配置文件的DTD信息 --&g ...

  4. Linux 守护进程和超级守护进程(xinetd)

    一 .Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的 ...

  5. iPhone取消软件更新上边的1

    去除设置的更新+1小红点提示主要分为越狱和非越狱设备两种方法. 越狱状态下方法: 首先将你的设备进行越狱: 越狱后安装ifile(这个自行搜索安装): 用ifile打开/System/Library/ ...

  6. call_user_func_array使用原型

    If you need to call object and class methods in PHP < 4.0.4, the following code ought to do the t ...

  7. ubuntu安装到选择位置时闪退

    转自:http://tieba.baidu.com/p/3020839207

  8. sc 与net命令的区别

    windows服务操作命令有sc和net 两个命令; sc stop serviceName  sc start serviceName net stop serviceName  net start ...

  9. 在eclipse中配置一个简单的spring入门项目

    spring是一个很优秀的基于Java的轻量级开源框架,为了解决企业级应用的复杂性而创建的,spring不仅可用于服务器端开发,从简单性.可测试性和松耦合性的角度,任何java应用程序都可以利用这个思 ...

  10. Spring配置JNDI的解决方案

    我的配置环境是:Spring + Tomcat + MySql 说明: 1. $TOMCAT_HOME代表Tomcat的安装目录. 第一步:在Tomcat的$TOMCAT_HOME/conf/cont ...