-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.py
42 lines (33 loc) · 1.2 KB
/
51.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
# leetcode 51 N皇后
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
result = []
chessBorad = ["." * n] * n
self.backTracking(n, 0, chessBorad, result)
return result
def backTracking(self, n, row, chessBorad, result):
if row == n:
result.append(chessBorad[:])
return result
for col in range(n):
if self.isValid(row, col, chessBorad):
chessBorad[row] = chessBorad[row][:col] + "Q" + chessBorad[row][col + 1:]
self.backTracking(n, row + 1, chessBorad, result)
chessBorad[row] = chessBorad[row][:col] + "." + chessBorad[row][col + 1:]
def isValid(self, row, col, chessBorad):
for i in range(row):
if chessBorad[i][col] == "Q":
return False
i, j = row - 1, col + 1
while i >= 0 and j < len(chessBorad[0]):
if chessBorad[i][j] == "Q":
return False
i -= 1
j += 1
i, j = row - 1, col - 1
while i >= 0 and j >= 0:
if chessBorad[i][j] == "Q":
return False
i -= 1
j -= 1
return True