[Google Round A APAC 2017] Problem B. Rain

15 篇文章 0 订阅

Problem

There's an island in the sea. The island can be described as a matrix with R rows and Ccolumns, with H[i][j] indicating the height of each unit cell. Following is an example of a 3*3 island:

3 5 5
5 4 5
5 5 5
Sometimes, a heavy rain falls evenly on every cell of this island. You can assume that an arbitrarily large amount of water falls. After such a heavy rain, some areas of the island (formed of one or more unit cells joined along edges) might collect water. This can only happen if, wherever a cell in that area shares an edge (not just a corner) with a cell outside of that area, the cell outside of that area has a larger height. (The surrounding sea counts as an infinite grid of cells with height 0.) Otherwise, water will always flow away into one or more of the neighboring areas (for our purposes, it doesn't matter which) and eventually out to sea. You may assume that the height of the sea never changes. We will use W[i][j] to denote the heights of the island's cells after a heavy rain. Here are the heights of the example island after a heavy rain. The cell with initial height 4 only borders cells with higher initial heights, so water will collect in it, raising its height to 5. After that, there are no more areas surrounded by higher cells, so no more water will collect. Again, note that water cannot flow directly between cells that intersect only at their corners; water must flow along shared edges.
Following is the height of the example island after rain:
3 5 5
5 5 5
5 5 5
Given the matrix of the island, can you calculate the total increased height sum(W[i][j]- H[i][j] ) after a heavy rain?

Input

The first line of the input gives the number of test cases, TT test cases follow.
The first line of each test case contains two numbers R and C indicating the number of rows and columns of cells on the island. Then, there are R lines of C positive integers each. The j-th value on the i-th of these lines gives H[i][j]: the height of the cell in the i-th row and the j-th column.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the total increased height.

Limits

1 ≤ T ≤ 100.
1 ≤ H[i][j] ≤ 1000.

Small dataset

1 ≤ R ≤ 10.
1 ≤ C ≤ 10.

Large dataset

1 ≤ R ≤ 50.
1 ≤ C ≤ 50.

Sample


Input 
 

Output 
 
3
3 3
3 5 5
5 4 5
5 5 5
4 4
5 5 5 1
5 1 1 5
5 1 5 5
5 2 5 8
4 3
2 2 2
2 1 2
2 1 2
2 1 2

Case #1: 1
Case #2: 3
Case #3: 0

Case 1 is explained in the statement.

In case 2, the island looks like this after the rain:

5 5 5 1
5 2 2 5
5 2 5 5
5 2 5 8

Case 3 remains unchanged after the rain.


题解:从矩形边界广搜,最小堆。

#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <cstring>
#include <set>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
struct node {
    int x,y,h;
    node(int xx,int yy,int hh) {
        x=xx;y=yy;h=hh;
    }
    node() {
    }
};
struct cmpp {
    bool operator() (node a,node b) {
       return a.h>b.h;
    }
};
int m,n,res;
int grid[51][51];
bool vis[51][51];
void caculate() {
    priority_queue<node,vector<node>,cmpp> q;
    memset(vis,true,sizeof(vis));
    for(int i=0;i<n;i++) {
        q.push(node(0,i,grid[0][i]));
        q.push(node(m-1,i,grid[m-1][i]));
        vis[0][i]=false;
        vis[m-1][i]=false;
    }
    for(int i=0;i<m;i++) {
        q.push(node(i,0,grid[i][0]));
        q.push(node(i,n-1,grid[i][n-1]));
        vis[i][0]=false;
        vis[i][n-1]=false;
    }
    int dir[][2]={-1,0,1,0,0,1,0,-1};
    while(!q.empty()) {
        node cur=q.top();
        q.pop();
        for(int i=0;i<4;i++) {
            node ne;
            ne.x=cur.x+dir[i][0];
            ne.y=cur.y+dir[i][1];
            if(ne.x<0||ne.x>m-1||ne.y<0||ne.y>n-1||!vis[ne.x][ne.y]) continue;
            vis[ne.x][ne.y]=false;
            ne.h=max(grid[ne.x][ne.y],cur.h);
            q.push(ne);
            res+=max(0,cur.h-grid[ne.x][ne.y]);
        }
    }
}
int main()
{
    //freopen("B-large-practice.in", "r", stdin);
    //freopen("B-large.out", "w", stdout);

    int t;
    cin>>t;
    for(int i=1;i<=t;i++) {
        cin>>m>>n;
        for(int ii=0;ii<m;ii++) {
            for(int jj=0;jj<n;jj++) {
                cin>>grid[ii][jj];
            }
        }
        res=0;
        caculate();
        cout<<"Case #"<<i<<": "<<res<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值