码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 38 Sudoku Solver

时间:2015-06-23 12:01:45      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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/

Leetcode 38 Sudoku Solver

标签:

原文地址:http://blog.csdn.net/andrew9tech/article/details/46602805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!