Algorithm for Eight Queens Problem

by Dinesh 2012-08-29 23:29:02

Algorithm for Eight Queens Problem


PROCEDURE Try(i: INTEGER; VAR q: BOOLEAN);
VAR j: INTEGER;
BEGIN j := 0;
REPEAT q := FALSE;
IF a[j] & b[i+j] & c[i-j+7] THEN
x[i] := j;
a[j] := FALSE; b[i+j] := FALSE; c[i-j+7] := FALSE;
IF i < 7 THEN
Try(i+1, q);
IF ~q THEN
a[j] := TRUE; b[i+j] := TRUE; c[i-j+7] := TRUE
END
ELSE q := TRUE
END
END ;
INC(j)
UNTIL q OR (j = Cool
END Try;
PROCEDURE Queens;
VAR i: INTEGER; (*uses global writer W*)
BEGIN
FOR i := 0 TO 7 DO a[i] := TRUE END ;
FOR i := 0 TO 14 DO b[i] := TRUE; c[i] := TRUE END ;
Try(0,q);
FOR i := 0 TO 7 DO Texts.WriteInt(W, x[i], 4) END ;
Texts.WriteLn(W)
END Queens.
881
like
0
dislike
0
mail
flag

You must LOGIN to add comments