题目:
题解:
class Solution:def solveSudoku(self, board: List[List[str]]) -> None:def dfs(pos: int):nonlocal validif pos == len(spaces):valid = Truereturni, j = spaces[pos]for digit in range(9):if line[i][digit] == column[j][digit] == block[i // 3][j // 3][digit] == False:line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = Trueboard[i][j] = str(digit + 1)dfs(pos + 1)line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = Falseif valid:returnline = [[False] * 9 for _ in range(9)]column = [[False] * 9 for _ in range(9)]block = [[[False] * 9 for _a in range(3)] for _b in range(3)]valid = Falsespaces = list()for i in range(9):for j in range(9):if board[i][j] == ".":spaces.append((i, j))else:digit = int(board[i][j]) - 1line[i][digit] = column[j][digit] = block[i // 3][j // 3][digit] = Truedfs(0)