pascal 细胞问题!

【问题描述】一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。如:阵列 0234500067103456050020456006710000000089有4个细胞。【输入】 输入共m+1行第一行有两个数据,分别表示总行数和总列数以下的m行,每行有n个0-9之间的数【输出】细胞个数【样例】输入文件(cell.in)4 100234500067103456050020456006710000000089输出文件(cell.out) 4程序 program xibao;const dx:array[1..4] of -1..1=(-1,0,1,0);dy:array[1..4] of -1..1=(0,1,0,-1);var int:text;name,s:string;pic:array[1..50,1..79] of byte;bz:array[1..50,1..79] of boolean;m,n,i,j,num:integer;h:array[1..4000,1..2] of byte;procedure doing(p,q:integer);var i,t,w,x,y:integer;begininc(num);bz[p,q]:=false;t:=1;w:=1;h[1,1]:=p;h[1,2]:=q;repeatfor i:=1 to 4 dobeginx:=h[t,1]+dx[i];y:=h[t,2]+dy[i];if (x>0) and (x<=m) and (y>0) and (y<=n) and bz[x,y]then begininc(w);h[w,1]:=x;h[w,2]:=y;bz[x,y]:=false;end;end;inc(t);until t>w;end;beginfillchar(bz,sizeof(bz),true);num:=0;assign(input,✀n:tp尀bin尀aaa.in✀);reset(input);readln(m,n);assign(output,✀n:tp尀bin尀aab.out✀);rewrite(output);for i:=1 to m dobegin readln(s);for j:=1 to n dobegin pic[i,j]:=ord(s[j])-ord(✀0✀);if pic[i,j]=0 then bz[i,j]:=false;end;end;for i:=1 to m dofor j:=1 to n do if bz[i,j] then doing(i,j);writeln(✀NUMBER of cells=✀,num);readln;close(input);close(output);end.哪位高人把每一部都讲解一下或者讲一下思路,本人感激不尽!!!
2026年09月24日 11:26
有1个网友回答
网友(1):

恩。。
看你的程序应该用的是种子填充法吧。。
首先读入数据(用ce[x,y])表示

for x:= 1 to n do
for y:= 1 to m do
如果 ce[x,y] > 0 则
doing(x,y)

procedure doing(x,y:integer):
begin
先把ce(x,y)标记为0;
储存答案的记数器sum +1 ;
扫描(x,y) 周围的4个点(x-1,y) (x+1,y) (x,y-1) (x,y+1);
如果某个点 > 0 那么 doing(这个点)
一直到把与x,y相关的点删去。。
end;

拿例子来说

0234500067
1034560500
2045600671
0000000089

首先扫描到了 x=1 y=2 处的2
doing(1,2)
我用[] 把待搜索的点标记 (此时(1,2)已经被标记为0了(即查找过(1,2);
0 0[3]4 5 0 0 0 6 7
1 0 3 4 5 6 0 5 0 0
2 0 4 5 6 0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

0 0 0[4]5 0 0 0 6 7
1 0[3]4 5 6 0 5 0 0
2 0 4 5 6 0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

0 0 0 0[5]0 0 0 6 7
1 0 0[4]5 6 0 5 0 0
2 0[4]5 6 0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

0 0 0 0 0 0 0 0 6 7
1 0 0 0[5]6 0 5 0 0
2 0 0[5]6 0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

0 0 0 0 0 0 0 0 6 7
1 0 0 0 0[6]0 5 0 0
2 0 0 0[6]0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

0 0 0 0 0 0 0 0 6 7
1 0 0 0 0 0 0 5 0 0
2 0 0 0 0 0 0 6 7 1
0 0 0 0 0 0 0 0 8 9

此时 已经把第一个细胞给找出来了并且把他从数据中删去