1751: [Usaco2005 qua]lake counting time limit:5 Sec Memory limit:64 MB
submit:190 solved:150
[Submit] [Status] [Discuss] Descriptiondue to recent rains, water have pooled in various places in Farmer John ' s field, which was represented by a recta Ngle of N x m (1 <= n <=; 1 <= M <=) squares. Each square contains the either water (' W ') or dry land ('. '). Farmer John would like to figure out how many ponds has formed in his field. A pond is a connected set of squares with water in them, where a square was considered adjacent to all eight of its NEIGHBO Rs. Given a diagram of Farmer John ' s field, determine how many ponds he had. 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 does 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 is three ponds:one in the upper left, one on the lower left,
And one along the right side.
HINT Source
Gold
The problem: The direct Meng Meng's DFS second, the classic popular Group Difficulty Foundation, water and water
(tip:38 dfs (A1,A2) seems to only write in this machine can be right, the submission can also be A; it is amazing if you write Dfs (I,J) in this machine will appear to bring in is (the) result in (2,2) Qaq, and then a variety of magical kneeling otl, Even more amazing is that this machine is kneeling into the dog's program can also be a (QAQ), pray for Ben explanation)
1 var2 I,j,k,l,m,n,a1,a2:longint;3 C1:char;4A:Array[0.. $,0.. $] ofLongint;5 procedureDFS (x,y:longint); inline;6 begin7a[x,y]:=0;8 ifa[x-1, Y1]=1 ThenDFS (X-1, Y1);9 ifa[x,y-1]=1 ThenDFS (x,y-1);Ten ifa[x+1, Y1]=1 ThenDFS (x+1, Y1); One ifa[x-1, y+1]=1 ThenDFS (X-1, y+1); A ifa[x,y+1]=1 ThenDFS (x,y+1); - ifa[x+1, y+1]=1 ThenDFS (x+1, y+1); - ifa[x-1, y]=1 ThenDFS (X-1, y); the ifa[x+1, y]=1 ThenDFS (x+1, y); - End; - begin - readln (n,m); +Fillchar (A,sizeof (a),0); - fori:=1 toN Do + begin A forj:=1 toM Do at begin - read (C1); - CaseC1 of - 'W': a[i,j]:=1; - '.': a[i,j]:=0; - End; in End; - Readln; to End; +l:=0; - fori:=1 toN Do the forj:=1 toM Do * ifa[i,j]=1 Then $ beginPanax Notoginsenga1:=i;a2:=J; - Inc (L);d FS (A1,A2); the End; + Writeln (l); A Readln; the End.
1751: [Usaco2005 qua]lake counting