332.重新安排行程
木有看懂,没视频所以也没看懂
51. N皇后
自己写出来还是有难度的
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
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 # 当前位置合法
37. 解数独
很有道理,但是一脱离模板就不会走路了
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
self.backtracking(board)
def backtracking(self, board):
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] != '.':
continue
for k in range(1, 10):
if self.isValid(i, j, k, board):
board[i][j] = str(k)
if self.backtracking(board):
return True
board[i][j] = '.'
return False
return True
def isValid(self, row, col, num, board):
for i in range(9):
if board[row][i] == str(num):
return False
if board[i][col] == str(num):
return False
start_col = (col // 3)*3
start_row = (row // 3)*3
for i in range(start_row, start_row+3):
for j in range(start_col, start_col+3):
if board[i][j] == str(num):
return False
return True
总结
标签:return,随想录,chessboard,51,self,算法,board,col,row From: https://www.cnblogs.com/miramira/p/18143612