Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!pipex!sunic!sics.se!roland
From: roland@sics.se (Roland Karlsson)
Subject: Re: N-Queens and Knights Tour Needed
In-Reply-To: knauf@theoinf.tu-ilmenau.de's message of 15 Dec 1994 14:16:30 GMT
Message-ID: <ROLAND.94Dec16101257@zith.sics.se>
Sender: news@sics.se
Organization: Swedish Institute of Computer Science, Kista
References: <3c0o1j$68n@yar.trincoll.edu> <3cpj3u$ikp@prakinf2.PrakInf.TU-Ilmenau.DE>
Date: Fri, 16 Dec 1994 09:12:57 GMT
Lines: 66


I am sorry to say but your queens program did not work.  I have made
several changes to it and included it below.  The naive permute and
test solution is also very inefficient for big boards.  You can find
several solutions to the queens and knights problem in:
  anonymous ftp: /archive/sicstus/muse0.6/queensANDknights.tar.Z
The most efficient solutions (in C and Prolog) can be found in
  ./Queens/wolz/{million_queens.c,queens.pl}
The Prolog programs is an improvement of my program:
  ./Queens/roland/queens.pl

BTW: the knights tour problem is the how to (starting in a corner)
visit all squares on a chess board exactly once with a knight.  My
program solves the problem deterministic for nearly all board sizes.
  ./Knights/roland/knights_tour.pl

---------------------------------------------------------------------------
	/*
        From: knauf@theoinf.tu-ilmenau.de (Dr. Rainer Knauf)
        Newsgroups: comp.lang.prolog
        Subject: Re: N-Queens and Knights Tour Needed
        Date: 15 Dec 1994 14:16:30 GMT

        Corrected by: roland@sics.se (Roland Karlsson)
        */

queens(N,L) :-
         possible_solution(N,L),
         correct_solution(L).

possible_solution(N,L) :-
         build_list(N,L1),
         permutation(L1,L).

build_list(0,[]) :- !.
build_list(N,[N|B]) :-
         N1 is N - 1,
         build_list(N1,B).

permutation([],[]).
permutation([H|B],L) :-
        permutation(B,L1),
        insert(H,L1,L).

insert(E,L,[E|L]).
insert(E,[H|B],[H|B1]) :-
        insert(E,B,B1).

correct_solution([]) :- !.
correct_solution([Q|Qs]) :-
        test_diags(Qs,Q,Q),
        correct_solution(Qs).

test_diags([],_,_) :- !.
test_diags([Q|Qs],Q1,Q2) :-
        Up is Q1 + 1,
        Q =\= Up,
        Down is Q2 - 1,
        Q =\= Down,
        test_diags(Qs,Up,Down).
--
-----------------------------------------------------------------------------
Roland Karlsson          Inet: roland@sics.se   Telex:    812 6154 7011 SICS
SICS, PO Box 1263,       Tel: +46 8 752 15 40   Ttx: 2401-812 6154 7011=SICS
S-164 28 KISTA, SWEDEN   Fax: +46 8 751 72 30
-----------------------------------------------------------------------------
