8 Queens Problem
Geoff Bien
gbien at intekom.co.za
Tue Jul 9 18:32:56 EDT 2002
<Simon.Foster at smiths-aerospace.com> wrote in message
news:mailman.1026221774.20130.python-list at python.org...
>
>
> Just For Fun!
>
> I've been working on a solution to the eigth queens puzzle.
> Here is my attempt. There's a solution in Scheme in SICP
> which takes only 13 lines. Can anyone come up with a shorter
> solution in Python, without any of the obvious silliness?
>
import string
def queens(n,lr,vert,rl,cols):
lr, rl = lr >> 1, rl << 1
if len(cols)==n:
print 'Solution: ', string.join(cols,', ')
for col, bitmask in [ (i+1, 0x01 << i) for i in range(n)]:
if not (bitmask & (lr | vert | rl)):
queens(n, lr | bitmask, vert | bitmask, rl | bitmask, cols+[`col`])
queens(8,0,0,0,[])
More information about the Python-list
mailing list