-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnQueens2.py
More file actions
28 lines (23 loc) · 958 Bytes
/
nQueens2.py
File metadata and controls
28 lines (23 loc) · 958 Bytes
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
"""
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
"""
class Solution:
def totalNQueens(self, n: int) -> int:
ans=0
left,upleft,lowleft=[0]*n,[0]*(2*n-1),[0]*(2*n-1)
def solve(col,board):
nonlocal ans
if col==n:
ans+=1
return
for row in range(n):
if not left[row] and not upleft[row+col] and not lowleft[n-1+row-col]:
board[row][col]="Q"
left[row],upleft[row+col],lowleft[n-1+row-col]=1,1,1
solve(col+1,board)
board[row][col]="."
left[row],upleft[row+col],lowleft[n-1+row-col]=0,0,0
board=[["." for i in range(n)] for _ in range(n)]
solve(0,board)
return ans