题解 | #Sudoku#
Sudoku
https://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
class Solution: def isValue(self, board, x, y): for i in range(9): # 检查列 if i != x and board[i][y] == board[x][y]: return False for j in range(9): if j != y and board[x][j] == board[x][y]: return False # 检查每个正方形是否符合 m, n = 3 * (x // 3), 3 * (y // 3) # 这里求出3x3网格的左上角的坐标 for i in range(3): for j in range(3): if (i + m != x or j + n != y) and board[i + m][j + n] == board[x][y]: return False return True def dfs(self,board): for i in range(9): for j in range(9): if board[i][j] == 0: for k in '123456789': board[i][j] = int(k) if self.isValue(board,i,j) and self.dfs(board): return True board[i][j] = 0 return False return True while True: try: board = [] for i in range(9): row = list(map(int, input().split())) board.append(row) s = Solution() s.dfs(board) for i in range(9): board[i] = list(map(str, board[i])) print(" ".join(board[i])) except: break#数独##我的实习求职记录#