Large algorithm issue -- 5x5 grid, need to fit 5 queens plus some squares
Lonnie Princehouse
finite.automaton at gmail.com
Thu Mar 16 07:56:29 CET 2006
It looks like a good start! Some tips-
- Index your arrays starting from 0 instead of 1. It will make life
easier (and it's the convention in most modern languages)
- Try a two dimensional array for the board representation? A list of
lists will do:
brd = [ [0] * 5 for i in xrange(5) ]
Now "brd[row][col]" will give the value of the square at that row and
column.
The printbrd function becomes:
def printbrd():
for row in xrange(5):
for col in xrange(5):
print brd[row][col],
print "" # print automatically adds a newline unless you
follow with a comma
- Or better yet, you don't even need a board representation. You can
only have five queens, as opposed to 25 squares, so instead of storing
a whole board, just store a list of the positions of queens.
- affect is going to wipe out previous queens "1" with a "3", and you
could still get a count of three or more zeros.
- Try to generalize the problem from 5 queesn on 5x5 to N queens on
NxN. Is 7 possible? How about 8?
- the permute function is a nice use of generators
More information about the Python-list
mailing list