-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheightQueens.py
More file actions
37 lines (32 loc) · 1.26 KB
/
eightQueens.py
File metadata and controls
37 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def conflict(state, nextX):
nextY = len(state)
for i in range(nextY):
if abs(state[i]-nextX) in (0, nextY-i):
return True
return False
def queens(num=8, state=()):
for pos in range(num):
if not conflict(state, pos):
if len(state) == num -1:
yield (pos, )
else:
for result in queens(num, state + (pos, )):
yield (pos, ) + result
def prettyPrint(solution):
def line(pos, length=len(solution)):
return '. '*(pos) + 'X ' + '. '*(length-pos-1)
for pos in solution:
print line(pos)
if __name__ == '__main__':
import random
prettyPrint(random.choice(list(queens(8))))
for solution in queens(8):
print solution
'''
作者:est
链接:https://www.zhihu.com/question/37046157/answer/70747261
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
'''
# 这是一个一行的解法
_=[__import__('sys').stdout.write("\n".join('.' * i + 'Q' + '.' * (8-i-1) for i in vec) + "\n===\n") for vec in __import__('itertools').permutations(xrange(8)) if 8 == len(set(vec[i]+i for i in xrange(8))) == len(set(vec[i]-i for i in xrange(8)))]