[leetcode]Sudoku Solver @ Python

原题地址:https://oj.leetcode.com/problems/sudoku-solver/

题意:

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

 

...and its solution numbers marked in red.

 

解题思路1:(这个版本在2014/09/12刚测试通过)

求数独的解,dancing links不会,只能写个位压缩版了。。

http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/#Sudoku_Solver

class Solution:
    # @param board, a 9x9 2D array
    # Solve the Sudoku by modifying the input board in-place.
    # Do not return any value.
    def solveSudoku(self, board):
        lt, rt, bt = [0] * 9, [0] * 9, [0] * 9
        self.dt = {}
        for i in range(9): self.dt[1<<i] = chr(ord('1')+i)
        for i in range(9):
            board[i] = list(board[i])
            for j in range(9):
                if (board[i][j] == '.'):
                    continue;
                num = ord(board[i][j]) - ord('1')
                lt[i] |= 1 << num
                rt[j] |= 1 << num
                bt[j//3*3+i//3] |= 1 << num
        self.dfs(board, 0, lt, rt, bt)
        board = [''.join(s) for s in board]
    
    def dfs(self, board, p, lt, rt, bt):
        while p < 81 and board[p/9][p%9] != '.':
            p += 1
        if p == 81:
            return True
        i, j, k = p//9, p%9, p%9//3*3+p//9//3
        if board[i][j] != '.':
            self.dfs(board, p + 1, lt, rt, bt)
            return True
        can = (~(lt[i]|rt[j]|bt[k])) & (0x1ff)
        pre = board[i]
        while can:
            num = can&-can
            board[i][j] = self.dt[num]
            lt[i] |= num
            rt[j] |= num
            bt[k] |= num
            if self.dfs(board, p + 1, lt, rt , bt):
                return True
            board[i][j] = '.'
            lt[i] &= ~num
            rt[j] &= ~num
            bt[k] &= ~num
            can -= num
        return False

 

解题思路2:(这个版本在2014/09/12刚测试, 超时,未通过)

使用dfs来解决问题。

http://www.cnblogs.com/zuoyuan/p/3770271.html

class Solution:
    # @param board, a 9x9 2D array
    # Solve the Sudoku by modifying the input board in-place.
    # Do not return any value.
    def solveSudoku(self, board):
        def isValid(x,y):
            tmp=board[x][y]; board[x][y]='D'
            for i in range(9):
                if board[i][y]==tmp: return False
            for i in range(9):
                if board[x][i]==tmp: return False
            for i in range(3):
                for j in range(3):
                    if board[(x/3)*3+i][(y/3)*3+j]==tmp: return False
            board[x][y]=tmp
            return True
        def dfs(board):
            for i in range(9):
                for j in range(9):
                    if board[i][j]=='.':
                        for k in '123456789':
                            board[i][j]=k
                            if isValid(i,j) and dfs(board):
                                return True
                            board[i][j]='.'
                        return False
            return True
        dfs(board)

 

posted on 2014-09-13 10:23  AIDasr  阅读(739)  评论(0编辑  收藏  举报

导航