8皇后问题 - python解 - Est's Blog

8皇后问题 - python解

看到javaboy在python版的发言 然后发现Raymond Hettinger的解

from itertools import permutations

n = 8 # number of bitches
cols = range(n)
for vec in permutations(cols):
    if n == len(set(vec[i]+i for i in cols)) \
         == len(set(vec[i]-i for i in cols)):
        print ("\n".join('.' * i + 'Q' + '.' * (n-i-1) for i in vec) + "\n===\n")

Comments