37. 解数独 - 力扣(LeetCode)
思路:
class Solution(object):
def back(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.back(board):
return True
board[i][j]='.'
return False
return True
def isValid(self,row,col,num,board):
word=str(num)
for i in range(9):
if board[row][i]==word:
return False
for i in range(9):
if board[i][col]==word:
return False
startRow=(row/3)*3
startCol=(col/3)*3
for i in range(startRow,startRow+3):
for j in range(startCol,startCol+3):
if board[i][j]==word:
return False
return True
def solveSudoku(self, board):
self.back(board)