forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_038.py
34 lines (25 loc) · 825 Bytes
/
problem_038.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
def is_valid(board, row):
if row in board:
return False
column = len(board)
for occupied_column, occupied_row in enumerate(board):
if abs(occupied_row - row) == abs(occupied_column - column):
return False
return True
def get_queen_configurations(board, n):
if n == len(board):
return 1
count = 0
for row in range(n):
if is_valid(board, row):
count += get_queen_configurations(board + [row], n)
return count
assert not is_valid([0, 2], 0)
assert not is_valid([0, 2], 2)
assert is_valid([0, 8], 3)
assert not is_valid([1, 3], 2)
assert is_valid([], 1)
assert get_queen_configurations([], 2) == 0
assert get_queen_configurations([], 4) == 2
assert get_queen_configurations([], 5) == 10
assert get_queen_configurations([], 8) == 92