-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_Queens.py
31 lines (25 loc) · 946 Bytes
/
N_Queens.py
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
29
30
31
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
cols, posD, negD = set(), set(), set()
res = set()
board = [['.' for i in range(n)] for i in range(n)]
print(board)
def backtrack(r):
if r == n:
temp = []
for row in board:
temp.append("".join(row))
res.add(tuple(temp))
for c in range(len(board[0])):
if c not in cols and (r+c) not in posD and (r-c) not in negD:
cols.add(c)
posD.add(r+c)
negD.add(r-c)
board[r][c] = 'Q'
backtrack(r+1)
cols.remove(c)
posD.remove(r+c)
negD.remove(r-c)
board[r][c] = '.'
backtrack(0)
return res