N-queens animation in SWI Prolog
Download Prolog code: queens.pl
Sample PostScript file, a self-contained saved animation
for 50 queens (open it with "gv" or "gs" to view
it): queens50.ps
You need SWI-Prolog >
5.6.50 (recommended: latest git version). Start it with
swipl -f queens.pl
You also need to have the PostScript viewer "gs" installed.
Here are a few example queries that you can try:
?- show(4, [], Qs).
?- show(8, [ff], Qs).
?- show(50, [ff], Qs).
?- show(50, [ff,down], Qs).
The first argument is the number of queens, the second argument is
a list of labeling options as described in SWI-Prolog's CLP(FD)
library documentation,
and the third argument is a list of row numbers, one for each
queen, such that they do not attack each other. As a side-effect,
you see an animation of the constraint solving process.
Here's an
intermediate state of the animation for 8 queens:
And here is a picture of a finished animation:
Example with 50 queens:
Challenges: Find solutions for many queens. Find good labeling and
allocation strategies. ...
Further reading:
Neumerkel
at
al., Visualizing
Solutions with Viewers.
Main page