-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00051-n_queens.py
63 lines (46 loc) · 1.39 KB
/
00051-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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 51: N Queens
# https://leetcode.com/problems/n-queens
from typing import List
class Solution:
# SOLUTION
def solveNQueens(self, n: int) -> List[List[str]]:
result = []
nQueens = [("." * n)] * n
def isValid(row: int, col: int) -> bool:
for i in range(row):
if nQueens[i][col] == 'Q':
return False
i, j = row-1, col-1
while i>=0 and j>=0:
if nQueens[i][j] == 'Q':
return False
i -= 1
j -= 1
i, j = row-1, col+1
while i>=0 and j<n:
if nQueens[i][j] == 'Q':
return False
i -= 1
j += 1
return True
def backtrack(row: int) -> None:
if (row == n):
result.append(nQueens.copy())
return
for col in range(n):
if isValid(row, col):
r = nQueens[row]
r = r[0:col] + "Q" + r[col+1:]
nQueens[row] = r
backtrack(row+1)
r = r[0:col] + "." + r[col+1:]
nQueens[row] = r
backtrack(0)
return result
if __name__ == "__main__":
o = Solution()
# INPUT
n = 4
# OUTPUT
result = o.solveNQueens(n)
print(result)