Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

POJ 2227 The Wedding Juicer (BFS+优先队列)

题意:给定n*m地图,其中高度不同,问最多能存多少水(和木桶一样,最外层不能存水)。

思路:黑书上有讲,从最外围的点开始加入到队列,每次取高度最小的点,然后看其子结点,如果高度大于自己那么加入队列,否则填充水量和自己高度相同,加入队列,可以证明这样做可以得到最优解,因为开始时最外围的点是不能够存水的,因此这个算法就是从"外"(高度最小)向"里"找,依次删除不能够存水的节点即可。

PS:  这个题做的太戏剧性了,敲完代码,发现机房机子中毒,exe文件没法执行(又要重装了.....),不能Debug,编译一遍检查无误,然后狠狠心裸交,结果有个变量未定义,返回RE...改完再交,结果AC........Ft,本来测试数据都不曾想过的...人品爆发了...

#include <iostream>
#include
<cstdio>
#include
<algorithm>
#include
<memory.h>
#include
<cmath>
#include
<bitset>
#include
<queue>
#include
<vector>
usingnamespace std;

constint BORDER = (1<<20)-1;
constint MAXSIZE =37;
constint MAXN =400;
constint INF =1000000000;
#define CLR(x,y) memset(x,y,sizeof(x))
#define ADD(x) x=((x+1)&BORDER)
#define IN(x) scanf("%d",&x)
#define OUT(x) printf("%d\n",x)
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))

#define SET_NODE(no,a,b,c) {no.x=a;no.y=b;no.h=c;}

typedef
struct{
int x,y;
int h;
}Node;
booloperator<(const Node& a,const Node& b)
{
return a.h > b.h;
}

Node t_node,node;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int n,m,ans;
int arr[MAXN][MAXN];
bool visit[MAXN][MAXN];

bool _is(constint& x,constint& y)
{
if(x <0|| x >= n)
returnfalse;
if(y <0|| y >= m)
returnfalse;
returntrue;
}
int init()
{
ans
=0;
CLR(visit,
0);
return0;
}
int input()
{
for(int i =0; i < n; ++i)
for(int j =0; j < m; ++j)
IN(arr[i][j]);
return0;
}
int bfs()
{
int i,j,x,y,h,a,b;
int corner[4][2] = {{0,0},{0,m-1},{n-1,0},{n-1,m-1}};
priority_queue
<Node> que;
while(!que.empty())
que.pop();
for(i =0; i <4; ++i)
{
visit[corner[i][
0]][corner[i][1]] =true;
SET_NODE(t_node,corner[i][
0],corner[i][1],
arr[corner[i][
0]][corner[i][1]]);
que.push(t_node);
}
for(i =1; i < n-1; ++i)
{
SET_NODE(t_node,i,
0,arr[i][0]);
que.push(t_node);
visit[i][
0] =true;
SET_NODE(t_node,i,m
-1,arr[i][m-1]);
que.push(t_node);
visit[i][m
-1] =true;
}
for(j =0; j < m; ++j)
{
SET_NODE(t_node,
0,j,arr[0][j]);
que.push(t_node);
visit[
0][j] =true;
SET_NODE(t_node,n
-1,j,arr[n-1][j]);
que.push(t_node);
visit[n
-1][j] =true;
}
/* while loop */
while(!que.empty())
{
node
= que.top();
que.pop();
a
= node.x;
b
= node.y;
h
= node.h;
for(i =0; i <4; ++i)
{
x
= a + dir[i][0];
y
= b + dir[i][1];
if(_is(x,y) &&!visit[x][y])
{
if(arr[x][y] < h)
{
ans
+= h - arr[x][y];
SET_NODE(t_node,x,y,h);
que.push(t_node);
}
else
{
SET_NODE(t_node,x,y,arr[x][y]);
que.push(t_node);
}
visit[x][y]
=true;
}
}
}
return ans;
}
int work()
{
bfs();
OUT(ans);
return0;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
init();
input();
work();
}
return0;
}

 

posted on 2010-04-24 17:35  Lvpengms  阅读(647)  评论(0编辑  收藏  举报