八皇后问题(Pascal)求改错
programeightqueen;constmaxn=8;varans:array[1..maxn,1..maxn]ofinteger;ha:array[1..maxn...
program eightqueen;
const maxn=8;
var ans:array[1..maxn,1..maxn] of integer;
ha:array[1..maxn] of integer;
zx:array[2..2*maxn] of integer;
fx:array[1-maxn..maxn-1] of integer;
total:integer;
procedure print;
var a,b:integer;
begin
writeln('No.',total);
for a:=1 to maxn do
begin
for b:=1 to maxn do
write(ans[a,b],' ');
writeln;
end;
end;
procedure search(i:integer);
var k,l,m,n:integer;
begin
if i>maxn then
begin
total:=total+1;
print;
end;
for k:=1 to 8 do
if (ha[k]=0) and (zx[i+k]=0) and (fx[i-k]=0) then
begin
ha[k]:=1;
zx[i+k]:=1;
fx[i-k]:=1;
ans[i,k]:=1;
search(i+1);
ans[i,k]:=0;
ha[k]:=0;
zx[i+k]:=0;
fx[i-k]:=0;
end;
end;
begin
fillchar(ans,sizeof(ans),0);
fillchar(ha,sizeof(ha),0);
fillchar(zx,sizeof(zx),0);
fillchar(fx,sizeof(fx),0);
total:=0;
search(1);
end.
在poj里总是WA,求改错... 展开
const maxn=8;
var ans:array[1..maxn,1..maxn] of integer;
ha:array[1..maxn] of integer;
zx:array[2..2*maxn] of integer;
fx:array[1-maxn..maxn-1] of integer;
total:integer;
procedure print;
var a,b:integer;
begin
writeln('No.',total);
for a:=1 to maxn do
begin
for b:=1 to maxn do
write(ans[a,b],' ');
writeln;
end;
end;
procedure search(i:integer);
var k,l,m,n:integer;
begin
if i>maxn then
begin
total:=total+1;
print;
end;
for k:=1 to 8 do
if (ha[k]=0) and (zx[i+k]=0) and (fx[i-k]=0) then
begin
ha[k]:=1;
zx[i+k]:=1;
fx[i-k]:=1;
ans[i,k]:=1;
search(i+1);
ans[i,k]:=0;
ha[k]:=0;
zx[i+k]:=0;
fx[i-k]:=0;
end;
end;
begin
fillchar(ans,sizeof(ans),0);
fillchar(ha,sizeof(ha),0);
fillchar(zx,sizeof(zx),0);
fillchar(fx,sizeof(fx),0);
total:=0;
search(1);
end.
在poj里总是WA,求改错... 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询