POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820
也可以参考黑书P89的积水。
题意:Farmer John有一个碗,形状如下,给你一个w*h平面,每个1*1的位置有一个高度Hij,问用这个碗来装牛奶,最多可以装多少体积。
例如:
555
515
555
这个可以装4体积,只有中间的1位置可以装;
再如:
555
512
555
这个可以装1体积,只有中间的1位置可以装,而装到2时就会开始漏出,因为液体是可以流动的;
分析:
由例子二可以很深刻的理解如果在一个桶边上打一些洞,那么之后这个桶能装的水取决于最低的那个洞的高度,
接着这个思路往下,对于最开始,只要找到这w*h外围的方格放入队列,这是最开始的边缘,取出其中最低的点x,
然后考察它周围4个位置中合法并且可以积水的点中的格子y,高度为Hy。
若y的高度大于x的高度,那么直接把Hy放入队列,并标记为禁止积水;
如果小于,把Hx放入队列,且ans+=Hx-Hy,意味着y位置可以放Hx-Hy体积的水,也标记为禁止积水。
然后再取禁止积水的格子中最低的,即队列中的最小值,继续向四周拓展。
核心思想:找最低边缘,把积过水的格子看成高度更新后的边缘。
由于要取最小高度,所以用到优先级队列,至于如何向四周扩展,我用的是bfs+dfs
#include <iostream>
#include <queue>
#include <stdio.h>
#include <algorithm> using namespace std;
const int maxn=;
int w,h;
long long block[maxn][maxn];
int mark[maxn][maxn]; //标记是否可以积水
long long ans=;
struct Node{
int x,y;
long long height;
bool operator<(const Node tmp)const{
return height>tmp.height;
}
}tmp;
priority_queue<Node> q;
//终于知道为什么AC不了了啊,原来之前的参数height写得是h,与整个board的高度h重复了额
void dfs(int x,int y,long long height){
if(x<||x>h||y<||y>w)
return;
mark[x][y]=;
ans+=height-block[x][y];
//上
if(x->&&mark[x-][y]){
if(height>block[x-][y])
dfs(x-,y,height);
else{
mark[x-][y]=;
tmp.x=x-;
tmp.y=y;
tmp.height=block[x-][y];
q.push(tmp);
}
}
//下
if(x+<=h&&mark[x+][y]){
if(height>block[x+][y])
dfs(x+,y,height);
else{
mark[x+][y]=;
tmp.x=x+;
tmp.y=y;
tmp.height=block[x+][y];
q.push(tmp);
}
}
//左
if(y->&&mark[x][y-]){
if(height>block[x][y-])
dfs(x,y-,height);
else{
mark[x][y-]=;
tmp.x=x;
tmp.y=y-;
tmp.height=block[x][y-];
q.push(tmp);
}
}
//右
if(y+<=w&&mark[x][y+]){
if(height>block[x][y+])
dfs(x,y+,height);
else{
mark[x][y+]=;
tmp.x=x;
tmp.y=y+;
tmp.height=block[x][y+];
q.push(tmp);
}
}
}
int main()
{
Node node;
scanf("%d%d",&w,&h);
for(int i=;i<=h;i++){
for(int j=;j<=w;j++){
mark[i][j]=;
scanf("%I64d",&block[i][j]); //存储高度
if(i==||i==h||j==||j==w){
mark[i][j]=; //表示禁止积水
node.x=i;
node.y=j;
node.height=block[i][j];
q.push(node);
}
}
}
ans=;
//bfs+dfs
while(!q.empty()){
node=q.top();
q.pop();
dfs(node.x,node.y,node.height);
}
cout<<ans<<endl;
return ;
}
下面给出只用队列+bfs的代码,速度稍慢一点:
参考链接:http://www.cnblogs.com/dongsheng/archive/2013/04/25/3043512.html
#include <iostream>
#include <queue>
#include <stdio.h>
#include <algorithm> using namespace std;
const int maxn=;
int w,h;
long long block[maxn][maxn];
int mark[maxn][maxn];
int dir[][] = {-,,,,,,,-};
long long ans=;
struct Node {
int x,y;
long long h;
bool operator<(const Node tmp)const {
return h>tmp.h;
}
} tmp;
priority_queue<Node> q;
long long BFS()
{
long long ans = ;
int i, j;
Node t1, t2;
while(!q.empty())
{
t1 = q.top();
q.pop();
for(int k = ; k < ; ++k)
{
i = t1.x + dir[k][];
j = t1.y + dir[k][];
if(i > && i <= h && j > && j <= w && mark[i][j])
{
if(block[i][j] < t1.h)
{
ans += t1.h - block[i][j];
t2.x = i;
t2.y = j;
t2.h = t1.h;
}
else
{
t2.x = i;
t2.y = j;
t2.h = block[i][j];
}
mark[i][j] = ;
q.push(t2);
}
}
}
return ans;
}
int main() {
Node node; //cin>>w>>h;
while(scanf("%d%d",&w,&h)!=EOF) {
for(int i=; i<=h; i++) {
for(int j=; j<=w; j++) {
mark[i][j]=;
scanf("%I64d",&block[i][j]);
if(i==||i==h||j==||j==w) {
mark[i][j]=; //表示禁止积水
node.x=i;
node.y=j;
node.h=block[i][j];
q.push(node);
}
}
}
ans=;
printf("%I64d\n",BFS());
}
return ;
}
POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)的更多相关文章
- POJ 3253 Fence Repair 贪心 优先级队列
		
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
 - 【POJ 3614 Sunscreen】贪心 优先级队列
		
题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头 ...
 - POJ:3083 Children of the Candy Corn(bfs+dfs)
		
http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...
 - 【BFS+优先级队列】Rescue
		
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/I [题意] 给定一个n*m的迷宫,A的多个小伙伴R要去营救A,问需要时间最少的小 ...
 - POJ 2431 Expedition 贪心 优先级队列
		
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30702 Accepted: 8457 Descr ...
 - ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
		
一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...
 - 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority queue)
		
堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...
 - 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
		
说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...
 - Java中的队列Queue,优先级队列PriorityQueue
		
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
 
随机推荐
- highcharts 显示点值的效果
			
plotOptions: { line: { /* <s:if test='#request.rdflags=="point"'> <s:if test=&quo ...
 - scp 跨机远程拷贝
			
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器. 命令格式: scp [参数] [原路径] [目标路径] ...
 - Call C# in powershell
			
How to call C# code in powershell Powershell Command Add-Type usage of Add-Type we use Add-Type -Typ ...
 - ultraedit高亮显示设置
			
ultraedit高亮显示设置 | 浏览:2333 | 更新:2014-02-20 23:05 1 2 3 4 5 6 7 分步阅读 百度经验:jingyan.baidu.com 写代码的人对ultr ...
 - Ubuntu系统安装配置Pintos和Bochs
			
Ubuntu系统安装配置 Pintos 和 Bochs 安装过程 首先是UEFI启动模式下Win8.1安装Ubuntu14.04双系统,由于篇幅过长,就不在这里详写.可见博主的另一篇博客http:// ...
 - Android -- Webview自适应屏幕
			
第一种 WebSett ...
 - kernel nf_conntrack: table full, dropping packet[转载]
			
http://blog.yorkgu.me/2012/02/09/kernel-nf_conntrack-table-full-dropping-packet/ 综合:ip_conntrack就是li ...
 - camera render texture 游戏里的监控视角
			
Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture camera里的视角会在材质球上出现 新建一个pla ...
 - 【转】eclipse技巧2
			
谈谈eclipse使用技巧二 上节说道了怎么使用eclipse使您事半功倍.这节告诉您怎么用eclipse练成火眼金睛. ①借你一双火眼金睛让类的层次结构一目了然让你阅读代码如虎添翼 一个好的类的层次 ...
 - python 可变参数
			
原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 一个最不常用的选择是可以让函数调用可变个数 ...