求细胞数量pascal题解
這題我是用廣度優(yōu)先搜索來(lái)做的
一開(kāi)始先一個(gè)一個(gè)點(diǎn)找,如果這個(gè)點(diǎn)是細(xì)胞,那么就搜索。
注意:可以把數(shù)組開(kāi)大點(diǎn)
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.
轉(zhuǎn)載于:https://www.cnblogs.com/YYC-0304/p/9500231.html
總結(jié)
以上是生活随笔為你收集整理的求细胞数量pascal题解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最优乘车pascal程序
- 下一篇: Oliver的救援pascal程序