-
Notifications
You must be signed in to change notification settings - Fork 9
/
Solution.py
30 lines (25 loc) · 1015 Bytes
/
Solution.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
# https://leetcode.com/problems/n-queens
class Solution:
def backtrack(self, n, i, blocked, board, ret):
if i == n:
ret.append([''.join(row) for row in board])
return
for j in range(n):
if blocked[i][j]: continue
board[i][j] = 'Q'
for k in range(1, n-i):
blocked[i+k][j] += 1
if j+k < n: blocked[i+k][j+k] += 1
if j-k >= 0: blocked[i+k][j-k] += 1
self.backtrack(n, i+1, blocked, board, ret)
board[i][j] = '.'
for k in range(1, n-i):
blocked[i+k][j] -= 1
if j+k < n: blocked[i+k][j+k] -= 1
if j-k >= 0: blocked[i+k][j-k] -= 1
def solveNQueens(self, n: int) -> List[List[str]]:
blocked = [[0 for i in range(n)] for j in range(n)]
board = [['.' for i in range(n)] for j in range(n)]
ret = []
self.backtrack(n, 0, blocked, board, ret)
return ret