-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathA3-Queens.py
More file actions
57 lines (49 loc) · 1.33 KB
/
A3-Queens.py
File metadata and controls
57 lines (49 loc) · 1.33 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# CPython encoding: utf-8
import time
t=time.clock()
def permute(L):
"Create variations to try"
n = len(L)
if n == 1:
for elem in L:
yield [elem]
else:
a = [L.pop(0)]
for p in permute(L):
for i in range(n):
yield p[:i] + a + p[i:]
def test(p, n):
"Check a variation"
for i in range(n - 1):
for j in range(i + 1, n):
d = p[i] - p[j]
if j - i == d or i - j == d:
return True
return False
def n_queen(n):
"N queens solver"
for p in permute(range(n)):
if not test(p, n): yield p
# Start columns from A
base_char = ord('A')
def print_board(solution):
"Display a board with a solution"
board = []
end = len(solution)
for row, pos in enumerate(solution):
board += ["\n%s %s ♕ %s" % ((end - row),
(' ☐ ' * pos),
(' ☐ ' * (end - pos - 1)))]
# Using character set hack!
board += '\n ' + \
' '.join([chr(base_char+i)
for i in range(0, end)])
return ''.join(board) + '\n'
def solve(n):
"Find all solutions"
for count, solution in enumerate(n_queen(n)):
print "Solution %d:" % (count + 1)
print print_board(solution)
return count
solve(8) # Normal chessboard size
print "Timing: ", time.clock()-t, "s"