home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Big Blue Disk 22
/
bbd22.zip
/
QUEENS-D.CHN
(
.txt
)
< prev
next >
Wrap
Turbo Pascal Chain module
|
1988-05-10
|
12KB
|
90 lines
Bad variable number.
C
C
C
C
C
Can you place eight
queens on a chess
board so that no two
attack each other?
Almost!
Press the spacebar to continue!
Positions Tested:
Queens Set:
END OF DEMO. !
return.chn
DEMO of QUEENS.PAS
)Enter a speed from 1 to 9 (1=slowest),
!or 0 to single step thru program
*Choose a display mode: Full or Overview.
Enter F or O
Press spacebar to continue...
0SPACE=continue, ESC=quit, 0-9 to change speed,
+ to change mode,
to scroll
InRow
InDiag1
InDiag2
QueenPos
Success
"{QUEENS.PAS = program to solve the
+ 8-queens problem by John Sigle 1/21/88 }
%PROGRAM EightQueens( Input, Output );
VAR K: INTEGER;
Success: Boolean;
" InRow: ARRAY[1..8] OF BOOLEAN;
* InDiag1: ARRAY[2..16] OF BOOLEAN; {/}
* InDiag2: ARRAY[-7..7] OF BOOLEAN; {\}
% QueenPos: ARRAY[1..8] OF INTEGER;
PROCEDURE Try( Col: INTEGER );
VAR Row: INTEGER;
,BEGIN { Try to place a queen in column Col }
Row := 0;
REPEAT { Try next row }
Row := Row+1;
Success := FALSE;
{ Test position }
) IF NOT(InRow[Row] OR InDiag1[Col+Row]
# OR InDiag2[Col-Row]) THEN
BEGIN
{ Set the queen }
QueenPos[Col] := Row;
InRow[Row] := TRUE;
! InDiag1[Col+Row] := TRUE;
! InDiag2[Col-Row] := TRUE;
IF Col<8 THEN
( BEGIN { Try to place another }
Try( Col+1 );
IF NOT Success THEN
* BEGIN { Remove this queen }
# QueenPos[Col] := 0;
$ InRow[Row] := FALSE;
* InDiag1[Col+Row] := FALSE;
) InDiag2[Col-Row] := FALSE
END
END
ELSE Success := TRUE
END
UNTIL Success OR (Row=8)
END; { of Try }
BEGIN { Main program }
{ Initialize indicators }
) FOR K := 1 TO 8 DO InRow[K] := FALSE;
+ FOR K := 2 TO 16 DO InDiag1[K] := FALSE;
+ FOR K := -7 TO 7 DO InDiag2[K] := FALSE;
( FOR K := 1 TO 8 DO QueenPos[K] := 0;
{ Try to find a solution }
TRY( 1 );
END. { of EightQueens }
Bad variable number.