POJ 2227 -- The Wedding Juicer(bfs+优先队列)
来源:互联网 发布:网络舆情软件 编辑:程序博客网 时间:2024/06/06 08:30
题目大意:一个W*H的网格,每个单位格的高度不一样,往这个网格注水,问能储存多少水;
思路分析:四周不能注水,和木桶原理一样,要以最低的高度作为能储水的高度,否则水就会溢出;将网格最外层的点开始加入队列,每次去高度最小的点作为“木桶”最低边,看其连接的点,如果高度大于自己加入队列,否则注水至自己的高度加入队列。
代码实现:
#include<cstdio>#include<cstring>#include<queue>using namespace std;int w[310][310],vis[310][310],n,m;int d[4][2]={1,0,-1,0,0,1,0,-1};long long sum;struct Node{ int x,y,h; Node(int xx=0,int yy=0,int hh=0):x(xx),y(yy),h(hh){} bool operator<(const Node &a)const{ return h>a.h; }};void bfs(){ priority_queue<Node> q; for(int i=1;i<=n;i++){ q.push(Node(1,i,w[1][i])); q.push(Node(m,i,w[m][i])); w[1][i]=-1; w[m][i]=-1; } for(int i=1;i<=m;i++){ q.push(Node(i,1,w[i][1])); q.push(Node(i,n,w[i][n])); w[i][1]=-1; w[i][n]=-1; } Node head; while(!q.empty()){ head=q.top(); q.pop(); //printf("h %d %d\n",head.x,head.y); for(int i=0;i<4;i++){ int x=head.x+d[i][0]; int y=head.y+d[i][1]; if(x<=1||x>=m||y<=1||y>=n) continue; if(w[x][y]==-1) continue; if(w[x][y]<head.h){ sum+=head.h-w[x][y]; //printf("*%d %d , %d %d\n",x,y,head.x,head.y); q.push(Node(x,y,head.h)); } else q.push(Node(x,y,w[x][y])); w[x][y]=-1; } } printf("%I64d\n",sum);}int main(){ while(~scanf("%d%d",&n,&m)){ memset(w,-1,sizeof(w)); memset(vis,0,sizeof(vis)); sum=0; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) scanf("%d",&w[i][j]); bfs(); }}
0 0
- POJ 2227 -- The Wedding Juicer(bfs+优先队列)
- poj 2227 The Wedding Juicer(优先队列+bfs)
- poj 2227 & usaco 2005 月赛 The Wedding Juicer 题解 bfs+优先队列
- poj 2227 The Wedding Juicer--floodfill--bfs
- poj 2227 The Wedding Juicer
- poj 2227 The Wedding Juicer
- poj 2227 & usaco 2005 月赛 The Wedding Juicer 题解
- [poj] The Wedding Juicer | [lintcode] Trapping Rain Water II
- 【bfs优先队列/floodfill】POJ 2227
- POJ 1724 ROADS(BFS + 优先队列)
- 【bfs优先队列】POJ 3635
- 【bfs优先队列】POJ 2312
- poj 2021 优先队列bfs
- poj 3635(bfs+优先队列)
- Poj 2312 BFS+优先队列
- POJ 2312(BFS+优先队列)
- POJ 3635 优先队列BFS
- POJ-2908:Quantum(BFS+优先队列(好题))
- 使用PHP生成二维码的两种方法(带logo图像)
- c语言 vs2013 安全检查
- ThreadLocal详解
- C++ STL学习之stack。
- 第五周项目三:-用多文件组织多个类的程序
- POJ 2227 -- The Wedding Juicer(bfs+优先队列)
- 毕晓英第二章17题
- hdu 1226 超级密码 BFS 挺不错的题啊!
- hihocoder#1015 : KMP算法
- 并发安全的Adapter
- Java代码 判断两个链表是否相交
- 1.10
- 进程和线程的联系和区别
- Alias Sampling Algorithm With GSL C代码实现