NYOJ InterestingPunch-Bowl

Dr.Kong has taken a side job designing interesting punch-bowl designs. The designs are created as follows:

      * A flat board of size W cm *  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!

Dr.Kong 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.
There are several test cases and end with the end of the file.

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

题意:一个N*M的“碗“,数字对应位置有一个高度的柱子,然后如果往里面无限加水,最多会存下多少水。


分析:一个位置能存下多少水是由四周最低的柱子高度决定的,所以我们先从碗一圈的位置高度存起来,然后找出最低的,推出四周四个位置的水高度,如果发现高度比本位置低的就说明次位置最多能存的水,比本位置高的说明存不下水。  然后把这些位置和最高高度存下来,然后重复就GG。


代码:

#include <bits/stdc++.h>
using namespace std;
int Map[305][305],n,m;;
struct node{
    int x,y,v;
};
struct cmp{
    int operator()(node x,node y){
        return x.v>y.v;
    }
};
priority_queue<node,vector<node>,cmp>q;
void solve(){
    int vis[305][305];
    long long ans=0;
    memset(vis,0,sizeof(vis));
    int move[4][2]={0,1 ,1,0 ,0,-1 ,-1,0};
    while(!q.empty()){
        node now=q.top();
       // printf("%d %d %d\n",now.x,now.y,now.v);
        q.pop();
        vis[now.x][now.y]=1;
        for(int i=0;i<4;i++){
            node next;
            next.x=now.x+move[i][0];
            next.y=now.y+move[i][1];
            next.v=Map[next.x][next.y];
            if(next.x<=0 || next.y<=0 || next.x>n || next.y>m || vis[next.x][next.y])continue;
            if(Map[next.x][next.y]<Map[now.x][now.y]){
                ans+=Map[now.x][now.y]-Map[next.x][next.y];
                Map[next.x][next.y]=Map[now.x][now.y];
                next.v=now.v;
            }
            vis[next.x][next.y]=1;
            q.push(next);
        }
    }
    printf("%lld\n",ans);
}
int main(){

    while(~scanf("%d%d",&m,&n)){
        while(!q.empty())q.pop();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&Map[i][j]);
                node t;
                t.x=i;t.y=j;t.v=Map[i][j];
                if(i==1 || j==1 || i==n || j==m)q.push(t);
            }
        }
        solve();
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值