-
Notifications
You must be signed in to change notification settings - Fork 0
/
9663.py
executable file
·45 lines (32 loc) · 973 Bytes
/
9663.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
# -*- coding: utf-8 -*-
import unittest
count = 0
def is_promised(queens: list, n: int) -> bool:
for c in range(n):
if queens[n] == queens[c] or abs(queens[n] - queens[c]) == abs(n - c):
return False
return True
def back_tracking(queens: list, length: int, current: int):
if current == length:
global count
count += 1
return
for c in range(length):
queens[current] = c
if is_promised(queens, current):
back_tracking(queens, length, current + 1)
return
def n_queen(length: int) -> int:
queens = [-1 for _ in range(length)]
back_tracking(queens, length, 0)
return count
class TestNQueen(unittest.TestCase):
def test_n_queen(self):
for length, expected in [
[4, 2], [8, 92]
]:
global count
count = 0
self.assertEqual(expected, n_queen(length))
if __name__ == '__main__':
unittest.main()