python遗传算法八皇后_python解决八皇后算法详解

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 in l and '*' or '_' for x in range(MAX)]

return board

# Construct the non-intersecting table

for pos in range(MAX):

intersecting_table[pos] = []

for row in range(NUM_QUEENS):

covered = range(row * NUM_QUEENS, (row+1) * NUM_QUEENS)

for pos in covered:

intersecting_table[pos] += covered

for col in range(NUM_QUEENS):

covered = [col + zerorow for zerorow in range(0, MAX, NUM_QUEENS)]

for pos in covered:

intersecting_table[pos] += covered

for diag in range(NUM_QUEENS):

l_dist = diag + 1

r_dist = NUM_QUEENS - diag

covered = [diag + (NUM_QUEENS-1) * x for x in range(l_dist)]

for pos in covered:

intersecting_table[pos] += covered

covered = [diag + (NUM_QUEENS+1) * x for x in range(r_dist)]

for pos in covered:

intersecting_table[pos] += covered

for diag in range(MAX - NUM_QUEENS, MAX):

l_dist = (diag % NUM_QUEENS) + 1

r_dist = NUM_QUEENS - l_dist + 1

covered = [diag - (NUM_QUEENS + 1) * x for x in range(l_dist)]

for pos in covered:

intersecting_table[pos] += covered

covered = [diag - (NUM_QUEENS - 1) * x for x in range(r_dist)]

for pos in covered:

intersecting_table[pos] += covered

universal_set = Set(range(MAX))

for k in intersecting_table:

non_intersecting_table[k] = universal_set - Set(intersecting_table[k])

# Once the non_intersecting_table is ready, the 8 queens problem is

# solved completely by the following method. Start by placing the

# first queen in position 0. Every time we place a queen, we compute

# the current non-intersecting positions by computing union of

# non-intersecting positions of all queens currently on the

# board. This allows us to place the next queen.

def get_positions(remaining=None, depth=0):

m = depth * NUM_QUEENS + NUM_QUEENS

if remaining is not None:

rowzone = [x for x in remaining if x < m]

else:

rowzone = [x for x in range(NUM_QUEENS)]

for x in rowzone:

if depth==NUM_QUEENS-1:

yield (x,)

else:

if remaining is None:

n = non_intersecting_table[x]

else:

n = remaining & non_intersecting_table[x]

for p in get_positions(n, depth + 1):

yield (x,) + p

return

rl = [x for x in get_positions()]

for i,p in enumerate(rl):

print '=' * NUM_QUEENS * 2, "#%s" % (i+1)

display(make_board(p))

print '%s solutions found for %s queens' % (i+1, NUM_QUEENS)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值