J Leaking Roof

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值