Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %---
- % Revision 3 of the queens solver.
- %---
- % A solved board is a board that has a piece in every row, and no piece
- % intersects any other (no rows are shared and no diagonals are shared).
- solvedBoardPerm(X) :- boardPerm(X), noIntersect(X).
- boardPerm([A,B,C,D,E,F,G,H]) :- row(A),row(B),row(C),row(D),row(E),row(F),row(G),row(H).
- row(A) :- between(0,7,A).
- % The head doesn't intersect anything in the tail, and there are no
- % intersections in the tail.
- noIntersect([]).
- noIntersect([Head | Tail]) :- noIntersect(Head, Tail, 1), noIntersect(Tail).
- noIntersect(_, [], _).
- noIntersect(Ele, [Head | Tail], DiagOffset) :-
- Ele \= Head, % The row is not shared.
- PosDiag is Head - DiagOffset, Ele \= PosDiag, % The positive diagonal is not shared.
- NegDiag is Head + DiagOffset, Ele \= NegDiag, % The negative diagonal is not shared.
- % The element doesn't intersect anything in the tail.
- NextDiagOffset is DiagOffset + 1, noIntersect(Ele, Tail, NextDiagOffset).
Advertisement
Add Comment
Please, Sign In to add comment