prolog下的eight queens

valid_queen((Row, Col)) :- 
Range = [1, 2, 3, 4, 5, 6, 7, 8], 
member(Row, Range), member(Col, Range).

valid_board([]).
valid_board([Head|Tail]) :- valid_queen(Head), valid_board(Tail).

rows([], []).
rows([(Row, _)|QueensTail], [Row|RowsTail]) :-
rows(QueensTail, RowsTail).

cols([], []).
cols([(_, Col)|QueensTail], [Col|ColsTail]) :-
cols(QueensTail, ColsTail).

diags1([], []).
diags1([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
Diagonal is Col - Row,
diags1(QueensTail, DiagonalsTail).

diags2([], []).
diags2([(Row, Col)|QueensTail], [Diagonal|DiagonalsTail]) :-
Diagonal is Col + Row,
diags2(QueensTail, DiagonalsTail).

eight_queens(Board) :-
length(Board, 8), 
valid_board(Board),

rows(Board, Rows),
cols(Board, Cols),
diags1(Board, Diags1),
diags2(Board, Diags2),

fd_all_different(Rows),
fd_all_different(Cols),
fd_all_different(Diags1),

fd_all_different(Diags2). 


依旧是很少的程序逻辑。一堆递归,一堆规则。最后给出一堆解。excellent language.

你可能感兴趣的:(Prolog)