5 queens

Terry Reedy tjreedy at udel.edu
Sat Dec 22 22:17:02 EST 2007


"John Machin" <sjmachin at lexicon.net> wrote in message 
news:ad789176-3e76-411c-bc18-7ce0c3785663 at s8g2000prg.googlegroups.com...
| It's *91* distinct solutions to what appears to be *exactly* your
| problem:
|
| """
| Dudeney (1970, pp. 95-96) also gave the following results for the
| number of distinct arrangements N_u(k,n) of k queens attacking or
| occupying every square of an nxn board for which no two queens attack
| one another (they are "not protected").
| k queens nxn N_u(k,n)
| 1 2 1
| 1 3 1
| 3 4 2
| 3 5 2
| 4 6 17
| 4 7 1
| 5 8 91
| """

If you don't want to work out everything for yourself, I would go to the 
Wolffram site and find the Dudeney reference and see if it has an algorithm 
or merely a list of solutions (even that would help).

A brute force search off the top of my head goes as follows:
The 'topmost' queen can potentially be in any column of rows 0 to 3;
  The second queen in the next row to 4, any column;
     Etc.
r8 = range(8)
for i0 in range(0, 4):
  for j0 in r8:
    for i1 in range(i0+1,5):
      for j1 in r8:
        for i2 in range(i1+1, 6):
           for j2 in r8:
              for i3 in range(i2+1,7):
                 for ji in r8:
                    for i4 in range(i3+1):
                       for j4 in r8:
                          test_position:

Optimizations: test non-attacking property as add each queen.
Change range of j0 to range(4) to delete reflections about vertical axis.

To detect all duplicates by reflection and rotation, one needs a 
'canonical' form for each position.  With that, I think one could do much 
better in not generating duplicates.

Terry Jan Reedy






More information about the Python-list mailing list