[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)
posted @ 2020-10-25 11:34  我的小叮当  阅读(74)  评论(0编辑  收藏  举报