52. N 皇后 II
为保证权益,题目请参考 52. N 皇后 II(From LeetCode).
解决方案1
Python
python
class Solution:
def totalNQueens(self, n: int) -> int:
self.n = n
self.cols = set()
self.diag = set()
self.bdiag = set()
self.ans: int = 0
self.dfs(0)
return self.ans
def dfs(self, row_idx: int):
if row_idx == self.n:
self.ans += 1
else:
for col_idx in range(self.n):
if col_idx in self.cols:
continue
diag_idx = row_idx - col_idx + (self.n - 1)
if diag_idx in self.diag:
continue
bdiag_idx = row_idx + col_idx
if bdiag_idx in self.bdiag:
continue
self.cols.add(col_idx)
self.diag.add(diag_idx)
self.bdiag.add(bdiag_idx)
self.dfs(row_idx + 1)
self.cols.remove(col_idx)
self.diag.remove(diag_idx)
self.bdiag.remove(bdiag_idx)
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
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