我做了一个八皇后,从算法上来看没有什么问题,我看了2天都没看出问题在哪?
只可以找到34种解,但是这34钟却是正确的,不知道为什么会漏掉这么多的解
program Project1;{$APPTYPE CONSOLE}uses
SysUtils;
const num=8;
type arra=record
row:integer;
col:integer;
end;
var
outCome :array[1..num,1..num] of char;
record1 :array[1..num] of arra;
count,i,j:integer;
function isSuit(col:integer;pos:integer):boolean;
var
i:integer;
begin
for i:= 1 to col do
if (abs(record1[i].row - col) = abs(record1[i].col - pos)) or (record1[i].col = pos) then
begin
isSuit:=false;
exit;
end;
isSuit:=true;
end;
procedure arrange(start:integer);
var
i:integer;
begin
if start>num then
begin
count:=count+1;
writeln(count);
end
else
begin
for i:=1 to num do
if isSuit(start,record1[i].row)=true then
begin
record1[start].col := i;
arrange(start+1);
end;
end;
end;
begin
count:=0;
for i:=1 to num do
for j:=1 to num do
outCome[i][j] := '*';
for i:= 1 to num do
record1[i].row := i;
arrange(1);
readln;
end.
只可以找到34种解,但是这34钟却是正确的,不知道为什么会漏掉这么多的解
program Project1;{$APPTYPE CONSOLE}uses
SysUtils;
const num=8;
type arra=record
row:integer;
col:integer;
end;
var
outCome :array[1..num,1..num] of char;
record1 :array[1..num] of arra;
count,i,j:integer;
function isSuit(col:integer;pos:integer):boolean;
var
i:integer;
begin
for i:= 1 to col do
if (abs(record1[i].row - col) = abs(record1[i].col - pos)) or (record1[i].col = pos) then
begin
isSuit:=false;
exit;
end;
isSuit:=true;
end;
procedure arrange(start:integer);
var
i:integer;
begin
if start>num then
begin
count:=count+1;
writeln(count);
end
else
begin
for i:=1 to num do
if isSuit(start,record1[i].row)=true then
begin
record1[start].col := i;
arrange(start+1);
end;
end;
end;
begin
count:=0;
for i:=1 to num do
for j:=1 to num do
outCome[i][j] := '*';
for i:= 1 to num do
record1[i].row := i;
arrange(1);
readln;
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货