Sunday, 10 July 2016

Google APAC 2017 University Test Round A Problem B. Rain (Spoj WATER - Water among Cubes)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva ), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

#define READ freopen("B-large-practice.in", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)

int h[55][55], w[55][55], r, c, dx[]={1, -1, 0, 0}, dy[]={0, 0, 1, -1};

int main()
{
    READ;
    WRITE;
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, n;

    cin>>tc;
    while(tc--)
    {
        cin>>r>>c;
        //CLR(visit);
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
            {
                cin>>h[i][j];
                if(i==1 || i==r || j==1 || j==c) w[i][j]=h[i][j]; //boundary
                else w[i][j]=1000; //inf
            }


        bool updateFound=true;
        while(updateFound)
        {
            updateFound=false;
            for(int i=2;i<r;i++)
                for(int j=2;j<c;j++)
                {
                    int mn=w[i][j];
                    for(int k=0;k<4;k++)
                    {
                        int x=i+dx[k];
                        int y=j+dy[k];
                        mn=min(mn, max(w[x][y], h[x][y]));
                    }
                    if(mn<w[i][j])
                    {
                        w[i][j]=mn;
                        updateFound=true;
                    }
                }
        }


        int ans=0;
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
                if(w[i][j]>h[i][j])
                    ans+=w[i][j]-h[i][j];

        cout<<"Case #"<<kk++<<": "<< ans <<"\n";
    }
    return 0;
}

1 comment: