-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_n_queens.py
57 lines (40 loc) · 1.44 KB
/
alg_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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
def _is_valid(queens):
"""Check current queen position is valid."""
current_row, current_col = len(queens) - 1, queens[-1]
# Check any queens can attack the current queen.
for row, col in enumerate(queens[:-1]):
col_diff = abs(current_col - col)
row_diff = abs(current_row - row)
if col_diff == 0 or col_diff == row_diff:
return False
return True
def n_queens(n, queens=[]):
"""The number of N Queens.
On an N by N board, get the number of possible arrangements of the board
where N queens can be placed on the board without attacking each other,
i.e. no two queens share the same row, column, or diagonal.
Time complexity: O(n!).
Space complexity: O(n).
"""
# queens is an 1-D array to store the column ids of queens.
if n == len(queens):
return 1
count = 0
for col in range(n):
# Append current queen's column id.
queens.append(col)
if _is_valid(queens):
# If current queen's position is valid, increment count by 1.
count += n_queens(n, queens)
# Backtrack by poping out current queen.
queens.pop()
return count
def main():
# Should be 1, 1, 0, 0, 2, 10, 4, 40, 92, 352.
for i in range(10):
print(n_queens(i))
if __name__ == '__main__':
main()