Round A APAC Test 2017 Problem B.Rain
2017-03-06 19:45
716 查看
Problem B. Rain
This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-StartGuide to get started.
Small input 9 points | Solve B-small Judge's response for last submission: Correct. |
Large input 15 points | Solve B-large Judge's response for last submission: Correct. |
Problem
There's an island in the sea. The island can be described as a matrix with R rows and C columns, 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 containingCase #x: y, where
xis the test case number (starting
from 1) and
yis 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 | Case #1: 1 Case #2: 3 Case #3: 0 |
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.
虽然是乱搞题。。。但是还有很多细节。。。自己没有想清楚就开写导致浪费了很多时间。。哎。。。
希望明天面试能够脑子清醒,好好表达自己。马友友!!娘娘!!!!give me power!!!
下面代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> using namespace std; const int maxn = 100; const int inf = 0x3f3f3f3f; int save[maxn][maxn]; bool vis[maxn][maxn]; int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; int n,m,sum; int mx,my; bool over(){ int i,j; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ if(!vis[i][j]){ return true; } } } return false; } bool check(int x,int y){ int i; for(i=0;i<4;i++){ if(!vis[x+dx[i]][y+dy[i]]){ return true; } } return false; } void select(){ int i,j; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ if(vis[i][j]&&check(i,j)&&save[mx][my]>save[i][j]){ mx = i; my = j; } } } } void dfs(int x,int y){ int i; for(i=0;i<4;i++){ int nx = x+dx[i], ny = y + dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny]){ if(save[nx][ny]<=save[mx][my]){ sum += save[mx][my] - save[nx][ny]; save[nx][ny] = save[mx][my]; vis[nx][ny] = true; dfs(nx,ny); }else{ vis[nx][ny] = true; } } } } int main(){ freopen("in.txt", "r", stdin); freopen("out.txt","w",stdout); int t,i,j,rnd = 1; scanf("%d",&t); while(t--){ sum = 0; scanf("%d%d",&n,&m); memset(vis,true,sizeof(vis)); for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ scanf("%d",&save[i][j]); vis[i][j] = false; } } for(i=1;i<=n;i++){ vis[i][1] = true; vis[i][m] = true; } for(i=1;i<m;i++){ vis[1][i] = true; vis [i] = true; } save[0][0] = inf; while(over()){ mx = 0, my = 0; select(); dfs(mx,my); } printf("Case #%d: %d\n",rnd++,sum); } return 0; }
相关文章推荐
- Practice Round APAC test 2017——Problem C. Not So Random
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem B. Robot Rock Band Google APAC 2017 University Test Practice Round
- Round B APAC Test 2017 Problem C. Watson and Intervals
- Problem A. Sherlock and Parentheses Google APAC 2017 University Test Round B
- Round D APAC Test 2017 Problem D. Stretch Rope (C++)
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem A. Country Leader Google APAC 2017 University Test Round A
- Round D APAC Test 2017 Problem A. Vote (C++)
- Round C APAC Test 2017 Problem A. Monster Path
- Practice Round APAC test 2017——Problem B. Robot Rock Band
- Round E APAC Test 2017 Problem B. Beautiful Numbers
- Problem B. Rain Google APAC 2017 University Test Round A
- Round C APAC Test 2017 Problem C. Evaluation
- Practice Round APAC test 2017——1.Problem A. Lazy Spelling Bee
- Problem C. Not So Random Google APAC 2017 University Test Practice Round
- Round A APAC Test 2017 Problem C. Jane's Flower Shop 二分、高精度要求
- Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
- Practice Round APAC test 2017——Problem D. Sums of Sums
- Round E APAC Test 2017 Problem A. Diwali lightings (C++)