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.
优先队列重载做法
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
struct node
{
int a,b;
int c;
bool operator < (const node &k) const
{
return c<k.c;
}
};
int dir[4][4]= {{1,0},{-1,0},{0,1},{0,-1} };
int high[505][505]= {0};
double water[505][505]= {0};
priority_queue< node > q;
int main()
{
int n;
double m;
scanf("%d%lf",&n,&m);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
water[i][j]=m;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
node s;
s.a=i,s.b=j;
scanf("%d",&s.c);
high[i][j]=s.c;
q.push(s);
}
for(int k=1; k<=n*n; k++)
{
node w=q.top();
q.pop();
double js=0;
for(int i=0; i<=3; i++)
if(w.a+dir[i][0]<=n&&w.a+dir[i][0]>=1&&w.b+dir[i][1]<=n&&w.b+dir[i][1]>=1&&w.c>high[w.a+dir[i][0]][w.b+dir[i][1]])
js++;
if(js==0)
continue;
double now=water[w.a][w.b]/js;
for(int i=0; i<=3; i++)
{
if(w.a+dir[i][0]<=n&&w.a+dir[i][0]>=1&&w.b+dir[i][1]<=n&&w.b+dir[i][1]>=1&&w.c>high[w.a+dir[i][0]][w.b+dir[i][1]])
water[w.a+dir[i][0]][w.b+dir[i][1]]+=now;
}
water[w.a][w.b]=0;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
if(high[i][j]==0)
printf("%lf ",water[i][j]);
else
printf("0 ");
cout<<endl;
}
}