Eight Queens Puzzle by Magnus Lie Hetland

Jeff Epler jepler at unpythonic.net
Wed Aug 13 10:50:38 EDT 2003


View the chessboard as an 8x8 grid.  Each square has an x and a y
coordinate which is an integer.

The vertical distance between (xi, yi) and (xj, yj) is abs(yi-yj).
Simularly, the horizontal distance is abs(xi-xj).

Two queens conflict if they are on the same row (identical y values), on
the same column (identical x values) or on the same diagonals.  To be on
the same diagonal, the horizontal and vertical distance will be the same.

The i'th queen is at the location (state[i], i), and the queen being
placed is at (nextX, nextY), and nextY > i.  So the 'if abs ... in
...:' test is identical to the description in the paragraph above, but
with the parts that are known impossible eliminated (same row) and the
calculation of the vertical distance eliminates the abs() call because
it is known to give a positive result.

Jeff





More information about the Python-list mailing list