拯救OIBH总部

本文提供了一道编程题目的背景和详细描述。题目中,oibh总部遭遇洪水,但部分区域因围墙(*号)阻挡洪水。任务是计算未被淹没的(0表示)重要区域的数量。解决方案包括读取地图,使用深度优先搜索(DFS)填充围墙内的水域,并最终输出未被淹没的0的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目背景

oibh总部突然被水淹没了!现在需要你的救援……

题目描述

oibh被突来的洪水淹没了>.<还好oibh总部有在某些重要的地方起一些围墙,用*号表示,而一个封闭的*号区域洪水是进不去的……现在给出oibh的围墙建设图,问oibh总部没被淹到的重要区域(由"0"表示)有多少。

输入输出格式

输入格式:

第一行是两个数,x和y(x,y<=500)

第二行及以下是一个由*和0组成的x*y的图。

输出格式:

输出没被水淹没的oibh总部的“0”的数量。

输入输出样例

输入样例#1:
样例输入1
4 5
00000
00*00
0*0*0
00*00

样例输入2
5 5
*****
*0*0*
**0**
*0*0*
*****

 

 

var map:array[0..550,0..550]of char;
n,m,j,i,ans:longint;
procedure flood(x,y:longint);
begin
    map[x,y]:='*';
    if (y-1>=1)and(map[x,y-1]='0')then flood(x,y-1);
    if (x+1<=n)and(map[x+1,y]='0')then flood(x+1,y);
    if (x-1>=1)and(map[x-1,y]='0')then flood(x-1,y);
    if (y+1<=m)and(map[x,y+1]='0')then flood(x,y+1);
end;
begin
    while not eof do
    begin
        readln(n,m);
        for i:=1 to n do
        begin
            for j:=1 to m do
              read(map[i,j]);
            readln;
        end;
        for i:=1 to n do
        begin
            if map[i,1]='0' then flood(i,1);
            if map[i,m]='0' then flood(i,m);
        end;
        for i:=1 to m do
        begin
            if map[1,i]='0' then flood(1,i);
            if map[n,i]='0' then flood(n,i);
        end;
        ans:=0;
        for i:=1 to n do
           for j:=1 to m do
             if map[i,j]='0' then inc(ans);
        writeln(ans);
    end;
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值