Grandpa Are lives in an old house. Now it is the heavy rain that occurs only once in a hundred years, but the roof of Grandpa Are's house leaks. His roof can be viewed as a n×n grid, and each square has a height. Denote the height of the square with coordinate (i,j) by hi,j meters. Now the rainfall is m millimeters, meaning that the incoming rain on every square is m millimeters.
Since the roof is not flat, the water flows, and the water on a square can only flow to an adjacent square with a strictly smaller height. Two squares are adjacent when they share a common edge. Also, since the water level is subtle compared with the difference in heights of the squares, water will NOT flow to adjacent squares with the same height due to surface tension. Furthermore, the water equally divides among all possible directions. Besides, the roof leaks. To be specific, if a square has a height of 0, it leaks.
Grandpa Are wants to know how much water flows to each leaking square after a long time. The water flows to a square includes the one from the incoming rain and the one from the adjacent squares. He asks you to calculate for him.
Input
The first line contains two integers n and m.
The next n lines contains n integers in each line. The i-th line contains integer hi,1,…,hi,n, separated by a space, meaning the height of the squares in the i-th row. There is a space in the end of each line, and there is an eoln in the end of the n-th line.
It is guaranteed that 1≤n≤500 and 0≤m,hi,j≤10000.
Output
There are n lines in the output, and each line contains n real numbers. The i-th line contains ai,1,…,ai,n, separated by a space, meaning the height of water flows to the corresponding square at last, or 0 if the square is not leaking.
Your answer will be considered correct if and only if the absolute or relative error of your answer to the correct answer is less than or equal to 10−6. Also, there is a space in the end of each line, and there is an eoln in the end of the n-th line.
Sample Input 1
3 1
0 1 2
1 2 3
2 3 4
Sample Output 1
9.000000 0 0
0 0 0
0 0 0
Sample Input 2
3 1
0 1 0
1 1 1
0 1 0
Sample Output 2
2.000000 0 2.000000
0 0 0
2.000000 0 2.000000
Notes
In the first sample, all water flows to the leaking square (1,1).
In the second sample, notice that the water in the square (2,2) will NOT flow. Although it is not intuitive, it is the case in this problem due to the imaginary surface tension.
题意: 有一个n*n的屋顶,每个屋顶都有一个高度h,如果当前位置的屋顶高度大于四周屋顶,就会把当前屋顶的水平均流入到四周比它矮的屋顶上。输入给出n和降雨量k,之后是n*n的屋顶高度,高度为0代表雨水可以漏下去,求各点漏下去的水量。
分析: 如果不排序直接处理很不方便,但降序排序后先遍历到高度高的点,这时不可能有比它更高的点更新它的水量,也就是当前点水量已经更新完毕,直接往四周流即可。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, m;
double water[505][505];
int h[505][505], _next[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
struct node
{
int x, y, h;
}p[250005];
bool cmp(node a, node b)
{
return a.h > b.h;
}
signed main()
{
cin >> n >> m;
int cnt = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
int t;
scanf("%d", &t);
h[i][j] = t;
p[++cnt].x = i;
p[cnt].y = j;
p[cnt].h = t;
water[i][j] = m;
}
sort(p+1, p+cnt+1, cmp);
for(int i = 1; i <= cnt; i++)
{
int cnt = 0, x = p[i].x, y = p[i].y, H = p[i].h;
for(int j = 0; j <= 3; j++)
{
int nextx = _next[j][0]+x, nexty = _next[j][1]+y;
if(nextx >= 1 && nextx <= n && nexty >= 1 && nexty <= n && h[nextx][nexty] < H)
cnt++;
}
if(cnt == 0)
continue;
for(int j = 0; j <= 3; j++)
{
int nextx = _next[j][0]+x, nexty = _next[j][1]+y;
if(nextx >= 1 && nextx <= n && nexty >= 1 && nexty <= n && h[nextx][nexty] < H)
water[nextx][nexty] += water[x][y]/cnt;
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
if(!h[i][j])
printf("%.6f ", water[i][j]);
else
printf("0 ");
puts("");
}
return 0;
}