pascal 的八皇后问题? 20
问下下面程序哪里错了?我感觉是判断能不能放那里错了,请问该怎么改?谢了!programtest;constn=4;varqueen:array[1..n]ofintege...
问下下面程序哪里错了?
我感觉是判断能不能放那里错了,请问该怎么改?谢了!
program test;
const
n=4;
var
queen:array [1..n] of integer;
function iscan(x,y:integer):boolean;
var
i:integer;
begin
iscan:=false;
for i:=1 to n do
if (abs(queen[i]-x) <> abs(i-y)) and (queen[i]<>x) and(y<>i) then
iscan:=true;
end;
procedure try(i:integer);
var
j:integer;
begin
for j:=1 to n do
if (iscan(j,i)) then begin
queen[i]:=j;
if i=n then
begin
for i:=1 to n do
begin
writeln(i,'is',queen[i]);
end;
writeln;
end
else
try(i+1);
end;
queen[i]:=0;
end;
begin
try(1);
readln;
end. 展开
我感觉是判断能不能放那里错了,请问该怎么改?谢了!
program test;
const
n=4;
var
queen:array [1..n] of integer;
function iscan(x,y:integer):boolean;
var
i:integer;
begin
iscan:=false;
for i:=1 to n do
if (abs(queen[i]-x) <> abs(i-y)) and (queen[i]<>x) and(y<>i) then
iscan:=true;
end;
procedure try(i:integer);
var
j:integer;
begin
for j:=1 to n do
if (iscan(j,i)) then begin
queen[i]:=j;
if i=n then
begin
for i:=1 to n do
begin
writeln(i,'is',queen[i]);
end;
writeln;
end
else
try(i+1);
end;
queen[i]:=0;
end;
begin
try(1);
readln;
end. 展开
1个回答
展开全部
program xy; var a:array[1..100]of integer; b,c,d:array[-100..100]of boolean; n,i,j,k:integer; procedure try(k:integer); var i:integer; begin if k>n then begin for j:=1 to n do write(a[j],' ');writeln;end else begin for i:=1 to n do if (b[i])and(c[k+i])and(d[k-i]){找条件} then begin a[k]:=i; b[i]:=false; c[k+i]:=false; d[k-i]:=false; try(k+1); b[i]:=true; c[k+i]:=true; d[k-i]:=true; end; end; end; begin readln(n); fillchar(b,sizeof(b),true); fillchar(c,sizeof(c),true); fillchar(d,sizeof(d),true); try(1); readln; end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询