Newsgroups: comp.ai
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!travelers.mail.cornell.edu!news.kei.com!news.mathworks.com!tank.news.pipex.net!pipex!howland.reston.ans.net!newsfeed.internetmci.com!news.sprintlink.net!in1.uu.net!netnews.jhuapl.edu!aplcenmp!hall
From: hall@aplcenmp.apl.jhu.edu (Marty Hall)
Subject: Re: How to construct of a solution of the N-Queen problem?
Message-ID: <DGK1Cs.F2C@aplcenmp.apl.jhu.edu>
Organization: JHU/APL AI Lab, Hopkins P/T CS Faculty
References: <45jhda$38i@news.ust.hk> <45lb9v$hqh@luxor.mch.sni.de> <poeck-1610951423550001@wi6a65.informatik.uni-wuerzburg.de>
Date: Mon, 16 Oct 1995 18:43:40 GMT
Lines: 42

In article <poeck-1610951423550001@wi6a65.informatik.uni-wuerzburg.de>
poeck@informatik.uni-wuerzburg.de (Karsten Poeck) writes:
>In article <45lb9v$hqh@luxor.mch.sni.de>, ifprolog@pgtr0158.mch.sni.de
>(IFCO) wrote:
>
>> Internally IF/Prolog uses constraint propagation to solve this - all
>> solutions can be found in a few seconds. For a 128x128 Queen board
>> IF/Prolog takes about 20 secs on a PC with the code below!

>Well, but iterative repair finds a -not all- solutions for the 1 Million
>queens problem in around a minute, does your method scale up for that
>size?

Hmm, although N-Queens serves as an interesting illustration of the
power of local search, constraint propagation, iterative repair, etc.,
IMHO it doesn't do all that much good to compare benchmarks too
closely. After all, as long as you just want one solution (not all
solutions) there are published equations that tell you where 
to place each queen for any N -- a closed form solution with no search
whatsoever, so that even 60 seconds for 1-million seems slow in
comparison. Ie printing out the positions takes a lot longer than
calculating them.

For instance, one algorithm from the SigArt Bulletin Vol 2 No 2 (early
80's?) gives this:

(A) For N even but not of form 6k+2:
  o Place first N/2 queens on col 2R. (R is Row number, 1 at top).
  o Place next N/2 queens on col 2(R-N/2)-1.
(B) For other even N cases
  o For I from 1 to N/2, calculate P=[(2(I-1) + N/2 -1)mod N]. Place the
    queen for Row I on P+1. Place the queen for row N-I+1 on N-P.
(C) For N odd
  o For rows 1 through N-1, put queen on same column as with N-1 board.
  o For row N, put queen on column N.

A very simple Lisp implementation of this, which prints out the board
itself (only useful for relatively small boards) is available from my
Lisp-hints page: htpp://www.apl.jhu.edu/~hall/lisp.html.

						- Marty
(proclaim '(inline skates))
