题目:
题解:
class Solution:def totalNQueens(self, n: int) -> int:def backtrack(row: int) -> int:if row == n:return 1else:count = 0for i in range(n):if i in columns or row - i in diagonal1 or row + i in diagonal2:continuecolumns.add(i)diagonal1.add(row - i)diagonal2.add(row + i)count += backtrack(row + 1)columns.remove(i)diagonal1.remove(row - i)diagonal2.remove(row + i)return countcolumns = set()diagonal1 = set()diagonal2 = set()return backtrack(0)