# 1763: 算法7-6：图的遍历——广度优先搜索

### 样例输入

4 0 0 0 1 0 0 1 1 0 1 0 1 1 1 1 0

### 样例输出

0 3 1 2

var a:array[1..51,1..51]of longint;
v:array[1..51]of boolean;
i,j,k,n,front,tail:longint;
procedure gb;
var i,j:longint;
begin
front:=1;
tail:=1;
fillchar(v,sizeof(v),false);
v[front]:=true;
write(front-1,' ');
while front<=tail do
begin
for i:=1 to n do
if (a[front,i]=1)and(v[i]=false)then
begin
inc(tail);
v[i]:=true;
write(i-1,' ');
end;
inc(front);
end;
end;
begin
for i:=1 to n do
for j:=1 to n do
gb;
writeln;
end.

var a:array[0..50,0..50] of longint;
i,j,k,n,s:longint;
v:array[0..50] of boolean;
b:array[0..50] of longint;
front,tail:longint;
procedure bfs(x:longint);
var j:integer;
begin
v[i]:=true;
b[tail]:=x;
while front<=tail do
begin
for j:=1 to n do
if (a[b[front],j]=1) and (v[j]=false) then
begin
inc(tail);
v[j]:=true;
b[tail]:=j;
end;
inc(front);
end;
end;
begin
for i:=1 to n do
for j:=1 to n do
for i:=1 to n do
if not v[i] then begin tail:=front; bfs(i); end;
for i:=1 to n do write(b[i]-1,' ');
writeln;
end.
program heyingjin;
var a:array[0..55,0..55]of integer;
e,i,j,k,top,bottom:integer;
visited:array[0..55]of boolean;
b:array[0..55]of integer;
begin
assign(input,'shuru.txt');
assign(output,'shuchu.txt');
reset(input);
rewrite(output);
for i:=0 to e-1 do
for j:=0 to e-1 do read(a[i,j]);
fillchar(visited,sizeof(visited),false);
top:=1;bottom:=1;
write(0);
while top<=bottom do
begin
k:=b[top];  visited[k]:=true;
for i:=0 to e-1 do
if (a[k,i]=1) and (not visited[i]) then
begin
inc(bottom);
b[bottom]:=i;
visited[i]:=true;
write(i,' ');
end;
inc(top);
end;
close(input);
close(output);
end.