-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path0051.py
75 lines (65 loc) · 2.43 KB
/
0051.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
64
65
66
67
68
69
70
71
72
73
74
75
from typing import List
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
res = []
# 一维列表
board = ['.' * n for _ in range(n)]
def isValid(board, row, col):
"""
检查是否有皇后互相冲突
"""
# 检查第 row 行 第 col 列是否可以放皇后
# 只需考虑 <= row,因为后面的棋盘是空的
for row_index in range(row):
# 判断当前行是否放了皇后
if row_index == row:
if 'Q' in board[row_index]:
return False
# 判断遍历每行时,第 col 列是否已经放了皇后
if 'Q' == board[row_index][col]:
return False
# 判断左上方是否放了皇后
tmp_row, tmp_col = row, col
while tmp_row > 0 and tmp_col > 0:
tmp_row -= 1
tmp_col -= 1
if 'Q' in board[tmp_row][tmp_col]:
return False
# 判断右上方是否放了皇后
tmp_row, tmp_col = row, col
while tmp_row > 0 and tmp_col < n - 1:
tmp_row -= 1
tmp_col += 1
if 'Q' in board[tmp_row][tmp_col]:
return False
return True
def replace_char(string, char, index):
"""
构建新的字符串进行赋值
"""
string = list(string)
string[index] = char
return ''.join(string)
def backtrack(board, row):
# 1.结束条件
if row == len(board):
# 需要用 list 转化一下
res.append(list(board[:]))
return
# 2.剪枝
# m = len(board[row])
for col in range(n):
# 剪枝
if not isValid(board, row, col):
continue
# 3.回溯并更新 row
board[row] = replace_char(board[row], 'Q', col)
# board[row][col] = 'Q'
backtrack(board, row + 1)
board[row] = replace_char(board[row], '.', col)
# board[row][col] = '.'
backtrack(board, 0)
return res
if __name__ == "__main__":
n = 4
print(Solution().solveNQueens(n))