UVA - 815 Flooded(浮点数二分)

题目链接:https://vjudge.net/problem/UVA-815

题意:有一个n*m(1<=n,m<30)的网格,每个格子是边长为10米的正方形,网格四周是无限高的墙壁。输入每个格子的海拔高度,以及网格内雨水的总体积,输出水位的海拔高度以及有多少百分比的区域有水(即高度严格小于水平面)。

解题方案:二分水位高度进行check,记所有网格中高度最低的格子的高度为a[0],当没有水的时候水位高度应该是a[0],此为水位高度的下限;当所有雨水都囤积到最低的格子处时,水位高度为a[0]+V/100.0,此为水位高度的上限,接下来在区间[a[0] , a[0]+V/100.0]进行二分check即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define FOR(i,k,n) for(int i=k;i<n;i++)
#define FORR(i,k,n) for(int i=k;i<=n;i++)
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mst(a,n)  memset(a,n,sizeof(a))
#define ll long long
#define N 1005
#define mod 1000000007
#define INF 0x3f3f3f3f

const double eps=1e-8;
const double pi=acos(-1.0);

int n,m;
double a[N];
double V;

bool check(double x)
{
    double sum=0;
    FOR(i,0,n*m)
    {
        if(a[i]<x)
            sum+=100.0*(x-a[i]);
    }
    //printf("%.5lf\n",sum);
    if(sum>=V) return true;
    else return false;
}

double found(double l,double r)
{
    while(fabs(l-r)>=eps)
    {
        double mid=(l+r)/2;
        if(check(mid))
            r=mid;
        else
            l=mid;
    }
    return l;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int Case=0;
    while(cin>>n>>m&&(n||m))
    {
        FOR(i,0,n*m) cin>>a[i];
        cin>>V;
        sort(a,a+n*m);
        double ans=found(a[0],a[0]+V/100.0);
        int cnt=0;
        FOR(i,0,n*m)
        {
            if(a[i]<ans) cnt++;
        }
        printf("Region %d\nWater level is %.2lf meters.\n%.2lf percent of the region is under water.\n\n"
               ,++Case,ans,(double)cnt*100.0/((double)n*m));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是八邻域种子蔓延洪水淹没算法的 Python 代码,其中也包含了计算区域淹没面积和受灾人口的部分: ```python import numpy as np def flood_fill(matrix, start_coords, fill_value): """ 使用八邻域种子蔓延洪水淹没算法填充矩阵 :param matrix: 需要填充的矩阵 :param start_coords: 填充的起始坐标 :param fill_value: 填充的值 """ # 获取矩阵的行数和列数 nrows, ncols = matrix.shape # 创建一个布尔类型的矩阵,用于记录每个位置是否已经被填充 filled = np.zeros((nrows, ncols)) # 创建一个列表,用于存储需要填充的位置坐标 fill_coords = [start_coords] # 开始填充 while fill_coords: # 取出需要填充的位置坐标 x, y = fill_coords.pop() # 如果该位置已经被填充,则跳过 if matrix[x, y] != fill_value: continue # 将该位置填充为指定的值 matrix[x, y] = fill_value # 标记该位置已经被填充 filled[x, y] = 1 # 找出该位置周围八个位置的坐标 neighbors = [(x+1, y), (x-1, y), (x, y+1), (x, y-1), (x+1, y+1), (x-1, y+1), (x+1, y-1), (x-1, y-1)] # 遍历每个周围的位置 for i, j in neighbors: # 如果该位置已经被填充,则跳过 if i >= 0 and j >= 0 and i < nrows and j < ncols: if not filled[i, j]: fill_coords.append((i, j)) # 计算淹没面积 flooded_area = np.sum(matrix == fill_value) # 计算受灾人口 affected_population = int(flooded_area * 0.002) return matrix, flooded_area, affected_population ``` 使用该函数可以得到填充后的矩阵、淹没面积和受灾人口。其中淹没面积的计算方法是统计矩阵中值等于填充值的个数,受灾人口的计算方法是淹没面积乘以一个系数(这里取0.2%)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值