TZOJ4521: Flooded Island(简单搜索判断、矩形最小边界最大边界)

文章讲述了如何解决一个关于海平面上升导致岛屿土地被淹没的问题,需要找出包含所有陆地的最小受破坏矩形区域。解题思路是遍历地图,检查每个陆地是否被至少3个海洋方块包围,然后输出更新后的最小边界矩形地图。

TZOJ4521: Flooded Island

题目传送门

描述

There is an beautiful island on the sea, but recently the sea level arround the island is raised, and continuously the land is disrupted. Now, we want to know what it will be like some years later.

The island is describled as a grid of R*C squares, each square has a character:

(1)‘X’: represents the land;

(2)‘.’: represents the sea.
If the land square is currently surrounded by at least 3 sea squares in four direction(north, south, east and west), it will be disrupted.

Now, please output the smallest disrupted map that contains all land squares.

There has at least one square of land will remain in all test cases.

We also assume that the map are surrounded by sea squares.

输入

The input case described the current map of the island.
The first line has two positive integers, R and C (1 <= R, C <= 10), then followed by R lines. Each line has C characters.

输出

Output the rectangluar map of the disrupted island that contains all land squares after some years later.

样例输入
5 3
...
.X.
.X.
.X.
...

样例输出
X
解题思路

本题主要还是题意理解,原题目中的样例给得不好理解,换成下面就好理解了。原题在洛谷
3 10

…XXX.XXX.
XXX…
输出
.XX…X
XX…
这里就是把周围有3个以上.(超出边界也算.)的X变成.,然后找出矩形的最小边界最大边界输出矩形图即可。

代码
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define inf 0x3f3f3f3f
#define IOS ios::sync_with_stdio(0), cin.tie(0)
const ll N = 1e1 * 1 + 5;
ll n, m;
char a[N][N], b[N][N];
ll dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if(a[i][j] == 'X') {
                ll ct = 0;
                for (int k = 0; k < 4; ++k) {
                    ll x = i + dir[k][0];
                    ll y = j + dir[k][1];
                    if(a[x][y] != 'X') ++ ct;
                }
                if(ct < 3) b[i][j] = 'X';
                else b[i][j] = '.';
            } else {
                b[i][j] = a[i][j];
            }
        }
    }
    ll u = inf, d = 0, l = inf, r = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if(b[i][j] == 'X') {
                u = min(u, i);
                d = max(d, i);
                l = min(l, j);
                r = max(r, j);
            }
        }
    }
    for (int i = u; i <= d; ++i) {
        for (int j = l; j <= r; ++j) {
            cout << b[i][j];
        }
        cout << endl;
    }
}

int main( ){
    IOS;
//    ll t; cin >> t;
    ll t = 1;
    while (t--) solve();
    return 0;
}
### 计算湖水最小淹没范围的方法 计算湖水的最小淹没范围通常涉及地理信息系统(GIS)、遥感技术和数值模拟技术的应用。以下是基于地形高程数据和湖泊初始条件的一种常见算法实现方式。 #### 数据准备 为了计算湖水的最小淹没范围,需要以下输入数据: 1. 高精度数字高程模型 (DEM),用于表示地面的高度分布[^3]。 2. 初始湖泊位置及其水面高度。 3. 地形中的障碍物或其他不可浸没区域的信息(如果适用)。 #### 方法概述 一种常用的技术是 **洪水填充算法** 或称为 **溢流扩散算法**,它通过逐步扩展水域边界直到达到指定的最大淹没深度或面积停止。这种方法的核心在于利用 DEM 的栅格化特性逐像素判断哪些区域会被淹没了。 ##### 算法步骤说明 - 初始化:设定初始湖泊的位置作为种子点集合 S 和当前水位 H0; - 扩展过程:迭代查找所有与现有水域相邻且海拔低于等于当前水位加一定增量 ΔH 的未访问网格单元,并将其加入到新的水域中;更新这些新增加部分对应的最高可能到达的新水位 Hi=max(Hi,Hi−1+Δh) ,其中 hi 表示局部坡度变化量; - 终止条件:当满足预设目标如总面积 A_target 或最大允许增加水量 V_max 时结束循环操作。 下面给出 Python 实现版本: ```python import numpy as np from scipy.ndimage import generic_filter def flood_fill(dem_array, start_points, max_water_level): """ Perform a flood fill algorithm to determine the minimum flooding area. Parameters: dem_array (numpy.ndarray): The digital elevation model array. start_points (list of tuples): List of starting coordinates for flooding. max_water_level (float): Maximum allowable water level during simulation. Returns: flooded_area_mask (numpy.ndarray): Boolean mask indicating flooded areas. """ rows, cols = dem_array.shape visited = np.zeros((rows, cols), dtype=bool) queue = [] for point in start_points: if not(0<=point[0]<rows and 0<=point[1]<cols): continue queue.append(point) while len(queue)>0: r,c=queue.pop(0) if visited[r][c]: continue current_elevation=dem_array[r][c] if current_elevation>max_water_level: break visited[r][c]=True neighbors=[(-1,0),(1,0),(0,-1),(0,1)] for dr,dc in neighbors: nr=r+dr;nc=c+dc if 0<=nr<rows and 0<=nc<cols and not visited[nr][nc]: queue.append((nr, nc)) return visited # Example usage with dummy data if __name__ == "__main__": # Create an example DEM grid dem_data=np.array([[5.,4.,3.,2.,1.], [6.,7.,8.,9.,10.], [11.,12.,13.,14.,15.]]) lake_start_positions=[(0,0)] # Starting position(s) within the lake boundary maximum_allowed_flooding_height=10.0 result=flood_fill(dem_data,lake_start_positions,maximum_allowed_flooding_height) print(result.astype(int)) ``` 此脚本定义了一个简单的 `flood_fill` 函数来执行基本的洪水蔓延逻辑,并演示了它的应用实例。注意实际项目可能会更加复杂并考虑更多细节比如处理不同类型的土壤渗透率等因素的影响等等[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值