题解 | #N皇后问题#
N皇后问题
https://www.nowcoder.com/practice/c76408782512486d91eea181107293b6
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型 the n
# @return int整型
#
class Solution:
def dps(self, i,n,has):
fob = {}
for item in has:
x,y = item
fob[y]=1
fob[y+i-x]=1
fob[y-(i-x)]=1
for j in range(n):
if j not in fob.keys():
hasnew=has+[(i,j)]
if i==n-1:
self.all.append(hasnew)
else:
self.dps(i+1,n,hasnew)
return 0
def Nqueen(self , n: int) -> int:
# write code here
self.all= []
self.dps(0,n,[])
return len(self.all)