这道题的题意就是一个n*n漏水的屋顶,问你每个漏水的地方会楼下多少的水。漏水的规则,每个平面都有相同数量的水,是从高往低,并且严格流向最低且低于当前的位置,当产生多个可能性时就平分该处的水。
这道题的思路就是把屋顶从高到低排序,可以用优先队列存储每个屋顶,然后利用宽搜处理最高出流向低处的水。
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 h
i,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 h
i,1
,…,h
i,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,h
i,j
≤10000.
Output
There are n lines in the output, and each line contains n real numbers. The i-th line contains a
i,1
,…,a
i,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.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
const int N = 510;
int n, m;
int H[N][N