SWI prolog and the Nth-Queens problem.
From: Cl?ment (google_at_philosophons.com)
Date: 04/24/04
- Next message: Hans Aberg: "Re: A simple (?) graph algorithm"
- Previous message: Nameless: "Re: bubblesort"
- Next in thread: Pento: "Re: SWI prolog and the Nth-Queens problem."
- Reply: Pento: "Re: SWI prolog and the Nth-Queens problem."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 24 Apr 2004 10:22:01 -0700
Hello,
I have 2 different questions to ask.
1) This is an interface question. I'm using SWI-Prolog 5.0.10, and I
have unbearable limitations in the main windows. The size (or the
buffer) of the windows seems to be limited : I can't go back up as
much as I would like to.
And, there seems to be a copy-paste problem with too big selection :
when I select a too big amount of text, SWI prolog bugs the selection.
Thoses problems dissalow me, for instance, to print a trace :(
Anyone know how to fix thoses problems ? Help would be very
appreciated, because this would make Prolog much more pleasant to
use...
2) I'm trying to solve the classical Nth Queens problem. This is the
following :
"Place N queens on a chessboard NxN so that no two queens are
attacking each other; i.e., no two queens are in the same row, the
same column, or on the same diagonal."
To simplify the problem, I'm first trying to solve it for N=4. I know
I could find the solution on the Internet, but I would like to find
one by myself, that's why I'm asking your help here :). And I really
don't understant why my programm isn't working (but I've done my best
to present it here as clearly as possible).
In my following programm, I Represent the positions of the queens as a
list of numbers from 1 to 4. Example: [1,3,4,2] means that the queen
in the first column is in row 1, the queen in the second column is in
row 3, etc...
queen([X1,X2,X3,X4]) :-
% Xi are numbers from 1 à 4.
member(X1, [1,2,3,4]),
member(X2, [1,2,3,4]),
member(X3, [1,2,3,4]),
member(X4, [1,2,3,4]),
% No repetitions (all the Xi are differents). That forbid %
the queens to be in the same raw.
X1 \= X2,
X1 \= X3,
X1 \= X4,
X2 \= X3,
X2 \= X4,
X3 \= X4,
% Forbid the diagonals.
% In general, 2 queens attacks each others if abs(Xi-Xj)= abs(i-j).
abs(X1-X2) \= abs(1-2),
abs(X1-X3) \= abs(1-3),
abs(X1-X4) \= abs(1-4),
abs(X2-X3) \= abs(2-3),
abs(X2-X4) \= abs(2-4),
abs(X3-X4) \= abs(3-4).
So, when I try "queen(X)."; I get the false answer [1,3,4,2]. What do
I have done wrong ? In my sense, the line "abs(X2-X3) \= abs(2-3)"
should have had dissalowed this solution :(
Thanks in advance for your help.
Regards,
Clement.
- Next message: Hans Aberg: "Re: A simple (?) graph algorithm"
- Previous message: Nameless: "Re: bubblesort"
- Next in thread: Pento: "Re: SWI prolog and the Nth-Queens problem."
- Reply: Pento: "Re: SWI prolog and the Nth-Queens problem."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]