博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1751: [Usaco2005 qua]Lake Counting
阅读量:7102 次
发布时间:2019-06-28

本文共 2687 字,大约阅读时间需要 8 分钟。

1751: [Usaco2005 qua]Lake Counting

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 190  Solved: 150
[][][]

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,
and one along the right side.

HINT

 

Source

 

题解:直接萌萌哒DFS秒之,经典的普及组难度基础题,水水哒

(Tip:38行的dfs(a1,a2)貌似只有这样写在本机才能对,提交也能A;很神奇的是如果直接写dfs(i,j)的话在本机就会出现带入的是(1,1)结果进去的是(2,2)QAQ,然后各种神奇跪OTL,更神奇的是这个在本机都跪成狗的程序居然submit之后也能A(QAQ),求神犇解释)

1 var 2    i,j,k,l,m,n,a1,a2:longint; 3    c1:char; 4    a:array[0..200,0..200] of longint; 5 procedure dfs(x,y:longint);inline; 6           begin 7                a[x,y]:=0; 8                if a[x-1,y-1]=1 then dfs(x-1,y-1); 9                if a[x,y-1]=1 then dfs(x,y-1);10                if a[x+1,y-1]=1 then dfs(x+1,y-1);11                if a[x-1,y+1]=1 then dfs(x-1,y+1);12                if a[x,y+1]=1 then dfs(x,y+1);13                if a[x+1,y+1]=1 then dfs(x+1,y+1);14                if a[x-1,y]=1 then dfs(x-1,y);15                if a[x+1,y]=1 then dfs(x+1,y);16           end;17 begin18      readln(n,m);19      fillchar(a,sizeof(a),0);20      for i:=1 to n do21          begin22               for j:=1 to m do23                   begin24                        read(c1);25                        case c1 of26                             'W':a[i,j]:=1;27                             '.':a[i,j]:=0;28                        end;29                   end;30               readln;31          end;32      l:=0;33      for i:=1 to n do34          for j:=1 to m do35              if a[i,j]=1 then36                 begin37                      a1:=i;a2:=j;38                      inc(l);dfs(a1,a2);39                 end;40      writeln(l);41      readln;42 end.

 

转载于:https://www.cnblogs.com/HansBug/p/4391198.html

你可能感兴趣的文章
磁盘调度策略
查看>>
【C#】扩展方法的应用
查看>>
case语法
查看>>
CentOS-6.7下安装Oracle11g
查看>>
spring的bean管理(注解方式)
查看>>
Struts2 Action 动态传参数
查看>>
Tomcat 安装webalizer
查看>>
bash的快捷键
查看>>
TCP连接状态详解及TIME_WAIT过多的解决方法
查看>>
12c datagurad 创建临时表空间遇到的问题
查看>>
ASM
查看>>
测试部署Ex2003和LCS2003
查看>>
跟我一起玩VSTS1--安装
查看>>
VMware内存分配初探
查看>>
【探索PowerShell 】【十一】函数
查看>>
Red Hat Linux 9安装Step By Step(3)
查看>>
MySQL数据库服务器逐渐变慢分析与解决
查看>>
[推荐]ORACLE SQL:经典查询练手第五篇(不懂装懂,永世饭桶!)
查看>>
03-4 BGP 默认路由/MED
查看>>
结合组策略和注册表对IE进行安全进阶配置
查看>>