-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathqueens.py
More file actions
27 lines (23 loc) · 749 Bytes
/
queens.py
File metadata and controls
27 lines (23 loc) · 749 Bytes
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
#/usr/bin/env python
import random
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 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 prettyprint(solution):
def line(pos, length = len(solution)):
return '. ' * (pos) + 'X ' + '. ' * (length - pos - 1)
for pos in solution:
print line(pos)
if __name__ == '__main__':
prettyprint(random.choice(list(queens(8))))