3385: [Usaco2004 Nov]Lake Counting 数池塘
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 22 Solved: 21[][][]Description
农夫约翰的农场可以表示成N×M(1≤N,M≤100)个方格组成的矩形.由于近日的降雨,
在约翰农场上的不同地方形成了池塘.每一个方格或者有积水(’W’)或者没有积水(’.’).农夫约翰打算数出他的农场上共形成了多少池塘.一个池塘是一系列相连的有积水的方格,每一个方格周围的八个方格都被认为是与这个方格相连的.
现给出约翰农场的图样,要求输出农场上的池塘数.
Input
第1行:由空格隔开的两个整数N和M.
第2到N+1行:每行M个字符代表约翰农场的一排方格的状态.每个字符或者是’W’或者
是’.’,字符之间没有空格.
Output
约翰农场上的池塘数.
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
HINT
共有3个池塘:一个在左上角,一个在左下角,还有一个沿着右边界
Source
题解:一开始居然WA了一下,结果发现子程序里面忘申请局部变量i了TT
别的实在没了,直接灌水秒之,不明白这个为啥也能成为金组。。。不过貌似NOV2004只有金组的= =
1 /************************************************************** 2 Problem: 3385 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:8 ms 7 Memory:344 kb 8 ****************************************************************/ 9 10 const dir:array[1..8,1..2] of longint=((0,1),(0,-1),(1,0),(-1,0),(1,-1),(-1,1),(1,1),(-1,-1));11 var12 i,j,k,l,m,n,ans:longint;13 ch:char;14 a:array[0..101,0..101] of longint;15 procedure floodfill(x,y:longint);16 var i:longint;17 begin18 if a[x,y]=0 then exit;19 a[x,y]:=0;20 for i:=1 to 8 do21 if a[x+dir[i,1],y+dir[i,2]]=1 then floodfill(x+dir[i,1],y+dir[i,2]);22 end;23 begin24 readln(n,m);25 fillchar(a,sizeof(a),0);26 for i:=1 to n do27 for j:=1 to m do28 begin29 read(ch);30 if upcase(ch)='W' then a[i,j]:=1;31 if j=m then readln;32 end;33 ans:=0;34 for i:=1 to n do35 for j:=1 to m do36 if a[i,j]=1 then37 begin38 inc(ans);39 floodfill(i,j);40 end;41 writeln(ans);42 readln;43 end.