-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ12_n_queens.py
63 lines (54 loc) · 1.85 KB
/
Q12_n_queens.py
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
58
59
60
61
62
63
import unittest
def ways_to_place_queens(n):
cols = [0] * n
diag_1 = [0] * ((2 * n) - 1)
diag_2 = [0] * ((2 * n) - 1)
positions = set()
output = []
def place_queen(row, col):
diag_1_idx = (col - row) + (n - 1)
diag_2_idx = col + row
cols[col] = 1
diag_1[diag_1_idx] = 1
diag_2[diag_2_idx] = 1
positions.add((row, col))
def remove_queen(row, col):
diag_1_idx = (col - row) + (n - 1)
diag_2_idx = col + row
cols[col] = 0
diag_1[diag_1_idx] = 0
diag_2[diag_2_idx] = 0
positions.remove((row, col))
def backtrack(row):
for col in range(n):
diag_1_idx = (col - row) + (n - 1)
diag_2_idx = col + row
if cols[col] == 0 and diag_1[diag_1_idx] == 0 and diag_2[diag_2_idx] == 0:
place_queen(row, col)
if row + 1 == n and sum(cols) == n:
add_output()
else:
backtrack(row + 1)
remove_queen(row, col)
def add_output():
res = []
for row in range(n):
res.append([])
for col in range(n):
if (row, col) in positions:
res[-1].append(1)
else:
res[-1].append(0)
output.append(res)
backtrack(0)
return output
class Test(unittest.TestCase):
def test_ways_to_place_queens(self):
four_solution = [
[[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 0]],
[[0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0]],
]
self.assertEqual([], ways_to_place_queens(3))
self.assertEqual(four_solution, ways_to_place_queens(4))
self.assertEqual(four_solution, ways_to_place_queens(4))
self.assertEqual(92, len(ways_to_place_queens(8)))