[Round A APAC Test 2017] B. Rain

作者:luozhipeng   发表日期:2016-7-10  浏览:986次

 

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, T. T 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 "stdafx.h"
#include<iostream>
#include<queue>
#define maxsize 55
#pragma warning(disable : 4996)
using namespace std;

int H[maxsize][maxsize];
int Map[maxsize][maxsize];

int R, C;

int di[4][2] = { 0, -1, 0, 1, 1, 0, -1, 0 };

struct Node
{
    friend bool operator<(Node n1, Node n2)
    {
        return n1.val > n2.val;
    }
    int i, j;
    int val;
};

void my_push(priority_queue<Node> &pri, int a, int b, int val)
{
    Map[a][b] = 1; //入队标记
    Node tn;
    tn.i = a;
    tn.j = b;
    tn.val = val;
    pri.push(tn);
}

int find_low(priority_queue<Node> &pri, int t)
{
    bool flag = false;
    while (!pri.empty())
    {
        Node ne = pri.top();
        pri.pop();

        if (ne.val > t)
            t = ne.val;

        for (int k = 0; k < 4; ++k)
        {
            int a = ne.i + di[k][0];
            int b = ne.j + di[k][1];

            if (a < 0 || a >= R || b < 0 || b >= C) //到达边界
            {
                flag = true;
                break;
            }

            if (Map[a][b] == 0)//没有访问过,入优先队列
            {
                my_push(pri, a, b, H[a][b]);
            }
        }

        if (flag)
            break;
    }

    return t;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int T;

    freopen("B-large.in", "r", stdin);
    freopen("B-large.out", "w", stdout);

    cin >> T;

    for (int t = 1; t <= T; ++t)
    {
        cin >> R >> C;
        for (int i = 0; i < R; ++i)
        {
            for (int j = 0; j < C; ++j)
            {
                cin >> H[i][j];
            }
        }

        int ans = 0;
        for (int i = 1; i < R - 1; ++i)
        {
            for (int j = 1; j < C - 1; ++j)
            {
                int t = H[i][j];

                memset(Map, 0, sizeof(Map));

                priority_queue<Node> pri;
                my_push(pri, i, j, t);

                int low = find_low(pri, t);

                ans += low - H[i][j];
            }
        }

        cout << "Case #" << t << ": " << ans << endl;

    }

    
    return 0;
}

标签:

本文固定链接: http://www.luozhipeng.com/?p=512
转载请注明: luozhipeng 2016-7-10 于 罗志鹏的BLOG 发表

上一篇: :下一篇
评论:请安装多说插件!
返回顶部