diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index bd1d1c9b..ab575bd8 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -348,48 +348,47 @@ class Solution { ```python class Solution: def solveNQueens(self, n: int) -> List[List[str]]: - if not n: return [] - board = [['.'] * n for _ in range(n)] - res = [] - def isVaild(board,row, col): - #判断同一列是否冲突 - for i in range(len(board)): - if board[i][col] == 'Q': - return False - # 判断左上角是否冲突 - i = row -1 - j = col -1 - while i>=0 and j>=0: - if board[i][j] == 'Q': - return False - i -= 1 - j -= 1 - # 判断右上角是否冲突 - i = row - 1 - j = col + 1 - while i>=0 and j < len(board): - if board[i][j] == 'Q': - return False - i -= 1 - j += 1 - return True + result = [] # 存储最终结果的二维字符串数组 + + chessboard = ['.' * n for _ in range(n)] # 初始化棋盘 + self.backtracking(n, 0, chessboard, result) # 回溯求解 + return [[''.join(row) for row in solution] for solution in result] # 返回结果集 + + def backtracking(self, n: int, row: int, chessboard: List[str], result: List[List[str]]) -> None: + if row == n: + result.append(chessboard[:]) # 棋盘填满,将当前解加入结果集 + return + + for col in range(n): + if self.isValid(row, col, chessboard): + chessboard[row] = chessboard[row][:col] + 'Q' + chessboard[row][col+1:] # 放置皇后 + self.backtracking(n, row + 1, chessboard, result) # 递归到下一行 + chessboard[row] = chessboard[row][:col] + '.' + chessboard[row][col+1:] # 回溯,撤销当前位置的皇后 + + def isValid(self, row: int, col: int, chessboard: List[str]) -> bool: + # 检查列 + for i in range(row): + if chessboard[i][col] == 'Q': + return False # 当前列已经存在皇后,不合法 + + # 检查 45 度角是否有皇后 + i, j = row - 1, col - 1 + while i >= 0 and j >= 0: + if chessboard[i][j] == 'Q': + return False # 左上方向已经存在皇后,不合法 + i -= 1 + j -= 1 + + # 检查 135 度角是否有皇后 + i, j = row - 1, col + 1 + while i >= 0 and j < len(chessboard): + if chessboard[i][j] == 'Q': + return False # 右上方向已经存在皇后,不合法 + i -= 1 + j += 1 + + return True # 当前位置合法 - def backtracking(board, row, n): - # 如果走到最后一行,说明已经找到一个解 - if row == n: - temp_res = [] - for temp in board: - temp_str = "".join(temp) - temp_res.append(temp_str) - res.append(temp_res) - for col in range(n): - if not isVaild(board, row, col): - continue - board[row][col] = 'Q' - backtracking(board, row+1, n) - board[row][col] = '.' - backtracking(board, 0, n) - return res ```