#P16577. [GKS 2016 #A] Rain

    ID: 16550 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>搜索2016Google Kick Start

[GKS 2016 #A] Rain

题目描述

There's an island in the sea. The island can be described as a matrix with RR rows and CC columns, with H[i][j]H[i][j] indicating the height of each unit cell. Following is an example of a 3×33\times 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 00.) 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]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 44 only borders cells with higher initial heights, so water will collect in it, raising its height to 55. 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]W[i][j]-H[i][j]) after a heavy rain?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow.

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

输出格式

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

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

提示

Limits

1T1001 \leq T \leq 100.

1H[i][j]10001 \leq H[i][j] \leq 1000.

Small dataset (Test set 1 - Visible)

1R101 \leq R \leq 10.

1C101 \leq C \leq 10.

Large dataset (Test set 2 - Hidden)

1R501 \leq R \leq 50.

1C501 \leq C \leq 50.