标签:基础算法 组成 inpu 模板 结构 medium 实现 数组 long
const
dx:array[1..4]of longint=(0,-1,0,1);
dy:array[1..4]of longint=(1,0,-1,0);
var
n,m,i,j,tj:longint;
a:array[-10..100,-10..100]of boolean;
x:char;
state:array[0..4000,0..4000]of longint;
procedure bfs(x,y:longint);
var
head,tail,i:longint;
begin
inc(tj);
a[x,y]:=false;
head:=0;tail:=1;
state[1,1]:=x;state[1,2]:=y;
repeat
inc(head);
for i:=1 to 4 do
begin
x:=state[head,1]+dx[i];
y:=state[head,2]+dy[i];
if (x>0)and(x<=m)and(y>0)and(y<=n)and(a[x,y]=true) then
begin
inc(tail);
state[tail,1]:=x;
state[tail,2]:=y;
a[x,y]:=false;
end;
end;
until head>=tail;
end;
begin
readln(m,n);
fillchar(a,sizeof(a),true);
for i:=1 to m do
begin
for j:=1 to n do
begin
read(x);
if x=‘0‘ then a[i,j]:=false;
end;
readln;
end;
for i:=1 to m do
for j:=1 to n do
if a[i,j]=true then bfs(i,j);
write(tj);
end.
// (该程序由网上搜集,要用到BFS,找到一个细胞后就进行BFS操作即可)
标签:基础算法 组成 inpu 模板 结构 medium 实现 数组 long
原文地址:http://www.cnblogs.com/LJY2002/p/7343856.html