每组测试数据第一行一个整数:n。其中n(1<=n<=30)
接下来n行,由0和1组成的nXn的方阵。
方阵内只有一个闭合圈,圈内至少有一个0。
已经填好数字2的完整方阵。
6 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1输出样例#1:
0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 2 2 1 1 1 2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 1
读入时把0改成2,然后每找到一个2,进行一次xfs。。。弄个队列把能达到的点都加进去,如果搜到边界,把队列里的赋为0
(机智如我)
const fx:array[1..4,1..2] of integer=((1,0),(0,1),(-1,0),(0,-1));
type sz=record
x:longint;
y:longint;
end;
var n,i,j,t,k:longint;
p:boolean;
f:array[1..900] of sz;
a,b:array[1..31,1..31] of longint;
procedure ok(x,y:longint);
var i,x1,y1:longint;
begin
for i:=1 to 4 do begin
x1:=x+fx[i,1];
y1:=y+fx[i,2];
if (x1>=1)and(x1<=n)and(y1>=1)and(y1<=n)and(a[x1,y1]=2)and(b[x1,y1]=0) then begin
inc(t);
f[t].x:=x1;
f[t].y:=y1;
b[x1,y1]:=1;
ok(x1,y1);
end else if (x1<1)or(x1>n)or(y1<1)or(y1>n) then begin
p:=false;
end;
end;
end;
begin
readln(n);
for i:=1 to n do begin
for j:=1 to n do begin
read(a[i,j]);
if a[i,j]=0 then a[i,j]:=2;
end;
readln;
end;
for i:=1 to n do begin
for j:=1 to n do begin
if a[i,j]=2 then begin
p:=true;
t:=1;
fillchar(f,sizeof(f),0);
fillchar(b,sizeof(b),0);
b[i,j]:=1;
f[1].x:=i;
f[1].y:=j;
ok(i,j);
if not p then for k:=1 to t do a[f[k].x,f[k].y]:=0;
end;
end;
end;
for i:=1 to n do begin
for j:=1 to n do write(a[i,j],' ');
writeln;
end;
{q1:=1;
q2:=1;
while q1<=q2 do begin
q3:=q2+1;
for i:=q1 to q2 do begin
end;
end; }
end.