1751: [Usaco2005 qua]Lake Counting
2015-04-03 22:46
531 查看
1751: [Usaco2005 qua]Lake Counting
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 190 Solved: 150
[Submit][Status][Discuss]
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 12W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
Sample Output
3OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,
and one along the right side.
HINT
Source
Gold题解:直接萌萌哒DFS秒之,经典的普及组难度基础题,水水哒
(Tip:38行的dfs(a1,a2)貌似只有这样写在本机才能对,提交也能A;很神奇的是如果直接写dfs(i,j)的话在本机就会出现带入的是(1,1)结果进去的是(2,2)QAQ,然后各种神奇跪OTL,更神奇的是这个在本机都跪成狗的程序居然submit之后也能A(QAQ),求神犇解释)
var i,j,k,l,m,n,a1,a2:longint; c1:char; a:array[0..200,0..200] of longint; procedure dfs(x,y:longint);inline; begin a[x,y]:=0; if a[x-1,y-1]=1 then dfs(x-1,y-1); if a[x,y-1]=1 then dfs(x,y-1); if a[x+1,y-1]=1 then dfs(x+1,y-1); if a[x-1,y+1]=1 then dfs(x-1,y+1); if a[x,y+1]=1 then dfs(x,y+1); if a[x+1,y+1]=1 then dfs(x+1,y+1); if a[x-1,y]=1 then dfs(x-1,y); if a[x+1,y]=1 then dfs(x+1,y); end; begin readln(n,m); fillchar(a,sizeof(a),0); for i:=1 to n do begin for j:=1 to m do begin read(c1); case c1 of 'W':a[i,j]:=1; '.':a[i,j]:=0; end; end; readln; end; l:=0; for i:=1 to n do for j:=1 to m do if a[i,j]=1 then begin a1:=i;a2:=j; inc(l);dfs(a1,a2); end; writeln(l); readln; end.
相关文章推荐
- [bzoj1751][Usaco2005 qua]Lake Counting
- 1751: [Usaco2005 qua]Lake Counting (搜索)
- bzoj1751 [Usaco2005 qua]Lake Counting
- bzoj 1751 [Usaco2005 qua]Lake Counting
- bzoj1751 [Usaco2005 qua]Lake Counting
- bzoj1751 [Usaco2005 qua]Lake Counting
- bzoj1755 [Usaco2005 qua]Bank Interest
- BZOJ[1750/3384]: [Usaco2005 qua]Apple Catching
- BZOJ 1755 [USACO 2005 Qua] Bank Interest 快速幂
- 1755: [Usaco2005 qua]Bank Interest
- bzoj1755 [Usaco2005 qua]Bank Interest
- 1754: [Usaco2005 qua]Bull Math
- 1754: [Usaco2005 qua]Bull Math (高精度乘法)
- BZOJ1753: [Usaco2005 qua]Who's in the Middle
- bzoj1754 [Usaco2005 qua]Bull Math
- 1753: [Usaco2005 qua]Who's in the Middle (快速排序)
- bzoj1750 [Usaco2005 qua]Apple Catching
- bzoj1753 [Usaco2005 qua]Who's in the Middle
- 1752: [Usaco2005 qua]Til the Cows Come Home (SPFA)
- bzoj1750 [Usaco2005 qua]Apple Catching