[Leetcode] 52. N皇后Ⅱ
题目链接:https://leetcode-cn.com/problems/n-queens-ii/
分析:
参考51.N皇后。
Python
class Solution:
def totalNQueens(self, n: int) -> int:
def solve(row, columns, diagnolL, diganolR):
if row == n:
return 1
else:
ans = 0
availablePos = ((1<<n)-1)&(~(columns|diagnolL|diganolR))
while availablePos:
column = availablePos & (-availablePos)
availablePos &= (availablePos-1)
ans += solve(row+1, columns|column, (diagnolL|column)<<1, (diganolR|column)>>1)
return ans
return solve(0, 0, 0, 0)