|
REM 8-QUEEN PROBLEM SOLVER
DECLARE EXTERNAL SUB SET_QUEEN
OPTION BASE 0
PUBLIC NUMERIC FLG0(7),FLG1(14),FLG2(14),QUEEN(7),COUNT_ANS
MAT FLG0 = ZER !列(|)方向に配置済みか
MAT FLG1 = ZER !右上左下方向に配置済みか
MAT FLG2 = ZER !左上右下方向に配置済みか
LET COUNT_ANS = 0 !答のカウント
CALL SET_QUEEN(0)
END
EXTERNAL SUB SET_QUEEN(I)
FOR J = 0 TO 7 !各行チェック
!3方向に未配置なら
IF(FLG0(J) = 0) AND (FLG1(I+J) = 0) AND (FLG2(I-J+7) = 0) THEN
LET QUEEN(I) = J !I行目のJ列目に配置
IF I = 7 THEN !全行配置完了なら
LET COUNT_ANS = COUNT_ANS + 1
PRINT COUNT_ANS !答の通し番号を表示
FOR K = 0 TO 7 !答を表示
PRINT REPEAT$("□",QUEEN(K)) & "■" & REPEAT$("□",7-QUEEN(K))
NEXT K
PRINT !一行空ける
ELSE
LET FLG0(J) = 1 !配置済み
LET FLG1(I+J) = 1
LET FLG2(I-J+7) = 1
CALL SET_QUEEN(I+1)
LET FLG0(J) = 0 !一つ下の行をチェック
LET FLG1(I+J) = 0 !するので未配置に戻す
LET FLG2(I-J+7) = 0
END IF
END IF
NEXT J
END SUB
|
|