找石油
Time Limit:1000MS Memory Limit:65536K
Total Submit:311 Accepted:163
Description
自从上次使用了你做的软件,YJQ公司被我们成功打败。他们只好另寻出路,去挖石油。为了完全打败YJQ公司,我们要把他的所有行动都封锁掉。现在我,LKI,又找到了你,编一个程序求出一块地中有多少块石油。
Input
第一行是这块地的长度与宽度N与M,(1<=N,M<=1000),下面N行M列的N*M个字符不是“@”,就是“#”。“@”代表着这是石油,“#”代表着这不是石油。若两块石油相邻,则认为他们是一块石油。
Output
只有一行,石油的个数。
Sample Input
4 3
@#@
@#@
##@
@@#
Sample Output
3
var
f:array[1..4,1..2]of longint;
a:array[0..1000,0..1000]of longint;
b:array[0..1000,0..1000]of boolean;
i,j,n,m,ans:longint;
x:ansistring;
function check(x,y:longint):boolean;
begin
if (x>0)and(x<=n)and(y>0)and(y<=m)and(a[x,y]=0)and(b[x,y]=true) then exit(true);
exit(false);
end;
procedure dfs(x,y:longint);
var i:longint;
begin
b[x,y]:=false;
for i:=1 to 4 do
if check(x+f[i,1],y+f[i,2]) then
dfs(x+f[i,1],y+f[i,2]);
exit;
end;
begin
readln(n,m);
f[1,1]:=0; f[1,2]:=1;
f[2,1]:=1; f[2,2]:=0;
f[3,1]:=0; f[3,2]:=-1;
f[4,1]:=-1; f[4,2]:=0;
for i:=1 to n do
begin
readln(x);
for j:=1 to length(x) do
if x[j]='#' then a[i,j]:=1
else a[i,j]:=0;
end;
fillchar(b,sizeof(b),true);
for i:=1 to n do
for j:=1 to m do
if (b[i,j])and(a[i,j]=0) then
begin
inc(ans);
dfs(i,j);
end;
write(ans);
end.