-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path52.py
42 lines (40 loc) · 1.47 KB
/
52.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
__________________________________________________________________________________________________
sample 28 ms submission
class Solution:
def totalNQueens(self, n: int) -> int:
self.count = 0
def DFS(n, row, cols, pie, na):
bits = (~(cols | pie | na)) & ((1 << n) - 1)
while bits:
p = bits & -bits
if row == n - 1:
self.count += 1
else:
DFS(n, row + 1,cols | p,(pie | p) << 1,(na | p) >> 1)
bits = bits & (bits - 1)
DFS(n,0,0,0,0)
return self.count
__________________________________________________________________________________________________
sample 12756 kb submission
class Solution:
def totalNQueens(self, n: int) -> int:
return self.backtrack([], n)
def backtrack(self, tmpSet, n):
if len(tmpSet) == n:
return 1
x = 0
for i in range(n):
if self.isValid(tmpSet, i):
tmpSet.append(i)
x += self.backtrack(tmpSet, n)
tmpSet.pop()
return x
def isValid(self, tmpSet, i):
n = len(tmpSet)
for j in range(len(tmpSet)):
if i == tmpSet[j]:
return False
if abs(n - j) == abs(i - tmpSet[j]):
return False
return True
__________________________________________________________________________________________________