% N-queens problem by Ilkka Niemelä; % modified by Tomi Janhunen to place queens row-by-row % % q(t,X,Y) gives the legal positions of the queens % d(1..queens): facts for the dimension of the board. % Place a queen on each row negq(X2,Y) :- q(X,Y), d(X), d(Y), d(X2), X2 != X. q(X,Y) :- not negq(X,Y), not q(X2,Y): d(X2): X2 != X , d(X), d(Y). hide negq(X,Y). % Make sure they don't threaten each other :- d(X), d(Y), d(Y1), q(X,Y), q(X,Y1), Y1 != Y. :- d(X), d(Y), d(X1), d(Y1), q(X,Y), q(X1,Y1), X != X1, Y != Y1, abs(X - X1) == abs(Y - Y1). d(1..queens). % Typical command line % lparse -1 -d none -c queens=8 queens.lp | smodels