python解决经典算法八皇后问题,在棋盘上放置8个皇后,而不让她们之间互相攻击。
import sys, itertools
from sets import Set
NUM_QUEENS = 8
MAX = NUM_QUEENS * NUM_QUEENS
# Each position (i.e. square) on the chess board is assigned a number
# (0..63). non_intersecting_table maps each position A to a set
# containing all the positions that are *not* attacked by the position
# A.
intersecting_table = {}
non_intersecting_table = {}
# Utility functions for drawing chess board
def display(board):
"Draw an ascii board showing positions of queens"
assert len(board)==MAX
it = iter(board)
for row in xrange(NUM_QUEENS):
for col in xrange(NUM_QUEENS):
print it.next(),
print '\n'
def make_board(l):
"Construct a board (list of 64 items)"
board = [x