Różnice
Różnice między wybraną wersją a wersją aktualną.
|
|
— |
pl:prolog:pllib:8_queens [2019/06/27 15:50] (aktualna) |
| ====== 8 queens ====== |
| {{tag>puzzle chess CLP}} |
| ===== Description ===== |
| A CLP(FD) program for eight queens. |
| |
| **Source**: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7. |
| ===== Download ===== |
| Program source code: {{prolog:pllib:8_queens.pl}} |
| ===== Listing ===== |
| <code prolog> |
| % Figure 14.9 A CLP(FD) program for eight queens. |
| |
| |
| % 8 queens in CLP(FD) |
| |
| solution( Ys) :- % Ys is list of Y-coordinates of queens |
| Ys = [_,_,_,_,_,_,_,_], % There are 8 queens |
| domain( Ys, 1, 8), % All the coordinates have domains 1..8 |
| all_different( Ys), % All different to avoid horizontal attacks |
| safe( Ys), % Constrain to prevent diagonal attacks |
| labeling( [], Ys). % Find concrete values for Ys |
| |
| safe( []). |
| |
| safe( [Y | Ys]) :- |
| no_attack( Y, Ys, 1), % 1 = horizontal distance between queen Y and Ys |
| safe( Ys). |
| |
| % no_attack( Y, Ys, D): |
| % queen at Y doesn't attack any queen at Ys; |
| % D is column distance between first queen and other queens |
| |
| no_attack( Y, [], _). |
| |
| no_attack( Y1, [Y2 | Ys], D) :- |
| D \= Y1-Y2, |
| D \= Y2-Y1, |
| D1 is D+1, |
| no_attack( Y1, Ys, D1). |
| </code> |
| ===== Comments ===== |
| |