Jump to content

N-queens minimum and knights and bishops: Difference between revisions

m
m (→‎{{header|J}}: remove an overly aggressive optimization..)
Line 458:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
At bishops I used a trick that at max only one row is left free.
<lang pascal>
<lang pascal>program TestMinimalQueen;
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
 
uses
sysutils;
const
KoorCOUNT = 16;
Line 474 ⟶ 477:
{$ALIGN 32}
CPG :tCheckPG;
solQsol,BSol,KSol :tPlayGround;
pgIdx,minIdx : nativeInt;
jumpedOver : boolean;
 
procedure pG_Out(pSol:tpPlayGround;ConvChar : string;lmt: NativeInt);
const
ConvChar = '_.Q';
var
row,col: NativeInt;
begin
iF length(ConvChar)<>3 then
EXIT;
for row := lmt downto 0 do
Begin
Line 552 ⟶ 556:
 
function check(lmt:nativeInt):boolean;
//check, if all fields are attacked
var
pPG :tpPlayGround;
Line 572 ⟶ 577:
EXIT;
inc(pgIdx);
//check every column
For col := 0 to lmt do
begin
//copy last state
CPG[pgIdx]:=CPG[pgIdx-1];
ifpPG := @CPG[pgIdx][row,col] <> 0 then;
if pPG^[row,col] <> 0 then
continue;
 
CPG[pgIdx]:=CPG[pgIdx-1];
RightAscDia(row,col,lmt);
LeftAscDia(row,col,lmt);
//set row and column as attacked
pPG := @CPG[pgIdx];
For i := 0 to lmt do
Begin
Line 586 ⟶ 594:
pPG^[i,col] := 1;
end;
//set position of queen
pPG^[row,col] := 2;
 
if check(lmt) then
begin
Line 592 ⟶ 602:
begin
minIdx := pgIdx;
solQsol := CPG[pgIdx]pPG^;
end;
end
Line 599 ⟶ 609:
BREAK
else
//check next rows
For t := row+1 to lmt do
SetQueen(t,lmt);
Line 605 ⟶ 616:
end;
 
procedure SetBishop(row,lmt: NativeInt);
var
pPG :tpPlayGround;
col,t: NativeInt;
begin
if pgIdx = minIDX then
EXIT;
inc(pgIdx);
For col := 0 to lmt do
begin
CPG[pgIdx]:=CPG[pgIdx-1];
pPG := @CPG[pgIdx];
if pPG^[row,col] <> 0 then
continue;
 
RightAscDia(row,col,lmt);
LeftAscDia(row,col,lmt);
 
//set position of bishop
pPG^[row,col] := 2;
 
if check(lmt) then
begin
if minIdx> pgIdx then
begin
minIdx := pgIdx;
Bsol := pPG^;
end;
end
else
if row > lmt then
BREAK
else
begin
//check next row
t := row+1;
if (t <= lmt) then
begin
SetBishop(t,lmt);
//left out max one row on field
if jumpedOver then
Begin
inc(t);
jumpedOver := false;
if t <= lmt then
SetBishop(t,lmt);
jumpedOver := true;
end;
end;
end;
end;
dec(pgIdx);
end;
 
var
lmt,max : NativeInt;
 
BEGIN
For lmtmax := 0 to 11 do10;
write(' nxn n=:');
For lmt := 1 to max do
write(lmt:3);
writeln;
 
write(' Queens :');
For lmt := 0 to max-1 do
begin
pgIdx := 0;
minIdx := 2*(lmt+1);
jumpedOver := true;
setQueen(0,lmt);
writelnwrite(lmt+1:3,minIDX:3);
// pG_Out(@sol,lmt);
end;
writeln;
pG_Out(@sol,lmt);
 
write(' Bishop :');
For lmt := 0 to max-1 do
begin
pgIdx := 0;
minIdx := 2*(lmt+1);
jumpedOver := true;
setBishop(0,lmt);
write(minIDX:3);
end;
writeln;
pG_Out(@Qsol,'_.Q',max-1);
writeln;
pG_Out(@Bsol,'_.B',max-1);
END.</lang>
{{out|@TIO.RUN}}
<pre>
nxn min. queens
1 1
2 1
3 2
4 3
5 3
6 4
7 4
8 5
9 5
10 5
11 6 //Real time: 3.241 s
12 7
.Q..........
............
........Q...
............
...Q........
............
............
..........Q.
............
.....Q......
..Q.........
Q...........
 
Real nxn time n=: 45.856 s1 CPU2 3 4 share: 99.245 % //6 below the7 time limit8 of 609 secs</pre>10
Queens : 1 1 2 3 3 4 4 5 5 5
Bishop : 1 2 3 4 5 6 7 8 9 10
..........
......Q...
..........
Q.........
..........
....Q.....
..........
........Q.
..........
..Q.......
 
 
B.........
.....B....
...B......
.....B....
...B......
........B.
....B.....
....B.....
....B.....
B.........
 
Real time: 4.740 s CPU share: 99.06 %</pre>
 
=={{header|Phix}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.