Sudoku Solver
class Solution: # @param {character[][]} board # @return {void} Do not return anything, modify board in-place instead. def solveSudoku(self, board): def check( x, y): temp = board[x][y]; board[x][y] = '.' for i in xrange(9): if board[i][y] == temp: return False for j in xrange(9): if board[x][j] == temp: return False for i in xrange(3): for j in xrange(3): if board[(x/3)*3 + i][(y/3)*3 + j] == temp: return False board[x][y] = temp return True def dfs(board): for i in xrange(9): for j in xrange(9): if board[i][j] == '.': for k in '123456789': board[i][j] = k if check(i, j) and dfs(board): return True board[i][j] = '.' return False return True dfs(board)
另一种方法:http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/
时间: 2024-10-04 02:32:07