主题:8queen八皇后问题(回溯算法)
这个程序到底有什么问题啊?我整天调试错误,能不能帮我解决一下?
program eightqueen;
var
a:array[1..8] of integer;
i,j,m:integer;
queen:boolean;
procedure print;
var t:integer;
begin
for t:=1 to 8 do write('(',t,',',a[t],') ');
writeln;
end;
procedure chk(x:integer);
begin
queen:=true;
for j:=1 to x-1 do if (a[j]=a[x]) or (abs(a[x]-a[j])=abs(x-j)) then queen:=false;
end;
procedure proc(x:integer);
begin
for i:=x+1 to 8 do a[i]:=1;
chk(x);
while (x<=8) and (a[x]<8) and (queen=false) do
begin
a[x]:=a[x]+1;
chk(x);
if (queen=false) or (a[x]>8) then
begin
while a[x]=8 do
begin
x:=x-1;
end;
a[x]:=a[x]+1;
chk(x);
end;
end;
if x<8 then proc(x+1) else
begin
print;
queen:=false;
proc(7);
end;
end;
begin
a[1]:=1;
proc(1);
end.
program eightqueen;
var
a:array[1..8] of integer;
i,j,m:integer;
queen:boolean;
procedure print;
var t:integer;
begin
for t:=1 to 8 do write('(',t,',',a[t],') ');
writeln;
end;
procedure chk(x:integer);
begin
queen:=true;
for j:=1 to x-1 do if (a[j]=a[x]) or (abs(a[x]-a[j])=abs(x-j)) then queen:=false;
end;
procedure proc(x:integer);
begin
for i:=x+1 to 8 do a[i]:=1;
chk(x);
while (x<=8) and (a[x]<8) and (queen=false) do
begin
a[x]:=a[x]+1;
chk(x);
if (queen=false) or (a[x]>8) then
begin
while a[x]=8 do
begin
x:=x-1;
end;
a[x]:=a[x]+1;
chk(x);
end;
end;
if x<8 then proc(x+1) else
begin
print;
queen:=false;
proc(7);
end;
end;
begin
a[1]:=1;
proc(1);
end.