The Wedding Juicer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2786 | Accepted: 1214 |
Description
Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows:
The blocks are all glued together carefully so that punch will not drain through them. They are glued so well, in fact, that the corner blocks really don't matter!
FJ's cows can never figure out, however, just how much punch their bowl designs will hold. Presuming the bowl is freestanding (i.e., no special walls around the bowl), calculate how much juice the bowl can hold. Some juice bowls, of course, leak out all the juice on the edges and will hold 0.
-
* A flat board of size W cm x H cm is procured (3 <= W <= 300, 3 <= H <= 300)
* On every 1 cm x 1 cm square of the board, a 1 cm x 1 cm block is placed. This block has some integer height B (1 <= B <= 1,000,000,000)
The blocks are all glued together carefully so that punch will not drain through them. They are glued so well, in fact, that the corner blocks really don't matter!
FJ's cows can never figure out, however, just how much punch their bowl designs will hold. Presuming the bowl is freestanding (i.e., no special walls around the bowl), calculate how much juice the bowl can hold. Some juice bowls, of course, leak out all the juice on the edges and will hold 0.
Input
* Line 1: Two space-separated integers, W and H
* Lines 2..H+1: Line i+1 contains row i of bowl heights: W space-separated integers each of which represents the height B of a square in the bowl. The first integer is the height of column 1, the second integers is the height of column 2, and so on.
* Lines 2..H+1: Line i+1 contains row i of bowl heights: W space-separated integers each of which represents the height B of a square in the bowl. The first integer is the height of column 1, the second integers is the height of column 2, and so on.
Output
* Line 1: A single integer that is the number of cc's the described bowl will hold.
Sample Input
4 5 5 8 7 7 5 2 1 5 7 1 7 1 8 9 6 9 9 8 9 9
Sample Output
12
题意:相当于给一个凹凸不平的底盘灌水,问可以存储多少水。
思路:从最外圈逐渐缩小底盘。先把最外圈的元素放进优先队列(进入过队列的元素都要标记已经处理),高度小的优先级高,那么队首元素就是最外圈高度最小位置,也即水
位的最大高度,如果再高,联通这层的容器里的水就会从这个最低的位置漏出去。
然后从这个最低的位置拓展(即广度优先搜索四个方向拓展,也就是黑书里面的floodfill方法),如果得到的位置水位小于等于当前高度,那么在答案里面加上水位差,同
时,将得到位置的高度置为当前水位的高度,然后入队(作为当前层容器的新边界),从而把边界缩小;如果高度比当前高,那么直接入队(它可能是内层容器的新边界)。
#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;
struct node
{
int x,y,h;
friend bool operator <(node a,node b)
{
return a.h > b.h; //h小的优先级高
}
};
int num[333][333], ans = 0, n, m;
bool vis[333][333];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
void process()
{
priority_queue<node>Q;
node t,t1;
for(int i = 1;i <= n;i ++) //将最外层的元素加入优先队列,并标记入过队
{ t.x = i;
t.y = 1; t.h = num[i][1];
Q.push(t);
vis[t.x][t.y] = true;
t.y = m; t.h = num[i][m];
Q.push(t);
vis[t.x][t.y] = true;
}
for(int i = 1;i <= m;i ++)
{
t.y = i;
t.x = 1; t.h = num[1][i];
Q.push(t);
vis[t.x][t.y] = true;
t.x = n; t.h = num[n][i];
Q.push(t);
vis[t.x][t.y] = true;
}
//逐渐向内层拓展即floodfill
while(!Q.empty())
{
t = Q.top();Q.pop();
for(int i = 0;i < 4;i ++)
{
t1.x = t.x + dir[i][0];
t1.y = t.y + dir[i][1];
if(t1.x <= 0 || t1.x > n || t1.y <= 0 || t1.y > m)continue;//位置越界
if(vis[t1.x][t1.y])continue; //此位置入过队
vis[t1.x][t1.y] = true; //标记入队
if(num[t1.x][t1.y] > t.h){
t1.h = num[t1.x][t1.y]; //所得位置高度大于当前位置入队
Q.push(t1);
continue;
}
t1.h = t.h; //所得位置高度小于当前位置,
ans += t.h - num[t1.x][t1.y];//先更新答案,再将所得位置置为当前高度,再入队
Q.push(t1);
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(vis,false,sizeof(vis)); //记录是否进入过队列
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++)
scanf("%d",num[i] + j);
ans = 0;
process();
printf("%d\n",ans);
}
return 0;
}