关于八皇后的问题...(pascal的)
programbahuanghou;varI:integer;Q:boolean;A:array[1..8]ofboolean;B:array[2..16]ofboole...
program bahuanghou;
var
I:integer;
Q:boolean;
A:array[1..8] of boolean;
B:array[2..16] of boolean;
C:array[-7..7] of boolean;
X:array[1..8] of integer;
procedure Try(I:integer; var Q:boolean);
var
J:integer;
begin
J:=0;
repeat
J:=J+1;Q:=False;
if A[J] and B[I+J] and C[I-J] then
begin
X[I]:=J;A[J]:=false;
B[I+J]:=false;C[I-J]:=false ;
if I<8 then
begin
Try(I+1,Q);
if not Q then
begin
A[J]:=true;B[I+J]:=true;C[I-J]:=true;
end
end
else
Q:=true;
end
until Q or (J=8)
end;
begin
for I:=1 to 8 do A[I]:=true;
for I:=2 to 16 do B[I]:=true;
for I:=-7 to 7 do C[I]:=true;
Try(1,Q);
if Q then
begin
writeln('one');
write(X[1]);
for I:=2 to 8 do
write(X[I]);
end;
readln
end.
请根据以上代码,写出较详细的运行过程..
eg:X[I]=1;A[J=?]=F;B[I+J=?]=F;C[I-J=?]=F;
X[I]=3; ... ...
(代码中变量的意思我基本都理解,但整个运行过程还是不太理解,用单步运行也看不出效果..orz)
请哪位好心人回答下吖!!!如果我看明白了再追加30分!! 展开
var
I:integer;
Q:boolean;
A:array[1..8] of boolean;
B:array[2..16] of boolean;
C:array[-7..7] of boolean;
X:array[1..8] of integer;
procedure Try(I:integer; var Q:boolean);
var
J:integer;
begin
J:=0;
repeat
J:=J+1;Q:=False;
if A[J] and B[I+J] and C[I-J] then
begin
X[I]:=J;A[J]:=false;
B[I+J]:=false;C[I-J]:=false ;
if I<8 then
begin
Try(I+1,Q);
if not Q then
begin
A[J]:=true;B[I+J]:=true;C[I-J]:=true;
end
end
else
Q:=true;
end
until Q or (J=8)
end;
begin
for I:=1 to 8 do A[I]:=true;
for I:=2 to 16 do B[I]:=true;
for I:=-7 to 7 do C[I]:=true;
Try(1,Q);
if Q then
begin
writeln('one');
write(X[1]);
for I:=2 to 8 do
write(X[I]);
end;
readln
end.
请根据以上代码,写出较详细的运行过程..
eg:X[I]=1;A[J=?]=F;B[I+J=?]=F;C[I-J=?]=F;
X[I]=3; ... ...
(代码中变量的意思我基本都理解,但整个运行过程还是不太理解,用单步运行也看不出效果..orz)
请哪位好心人回答下吖!!!如果我看明白了再追加30分!! 展开
1个回答
展开全部
program bahuanghou;
var a:array[0..100]of integer;
n,i,k:integer;
procedure start;
begin
assign(input,'bahuanghou.in');
reset(input);
assign(output,'bahuanghou.out');
rewrite(output);
end;
procedure print;
begin
for i:=1 to n do write(a[i]);
writeln;
k:=k+1;
end;
procedure trying(i:integer);
var j,k:integer;
f:boolean;
begin
for j:=1 to n do
begin
f:=true;
for k:=1 to i-1 do
if(a[k]=j)or(k+a[k]=i+j)or(k-a[k]=i-j)then
begin f:=false;break;end;
if f then begin
a[i]:=j;
if i=n then print
else trying(i+1);
end;
end;
end;
begin
start;
readln(n);
for i:=1 to n do a[i]:=0;
trying(1);
write(k);
close(input);close(output);
end.
这个是否好懂点
var a:array[0..100]of integer;
n,i,k:integer;
procedure start;
begin
assign(input,'bahuanghou.in');
reset(input);
assign(output,'bahuanghou.out');
rewrite(output);
end;
procedure print;
begin
for i:=1 to n do write(a[i]);
writeln;
k:=k+1;
end;
procedure trying(i:integer);
var j,k:integer;
f:boolean;
begin
for j:=1 to n do
begin
f:=true;
for k:=1 to i-1 do
if(a[k]=j)or(k+a[k]=i+j)or(k-a[k]=i-j)then
begin f:=false;break;end;
if f then begin
a[i]:=j;
if i=n then print
else trying(i+1);
end;
end;
end;
begin
start;
readln(n);
for i:=1 to n do a[i]:=0;
trying(1);
write(k);
close(input);close(output);
end.
这个是否好懂点
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询