Leetcode 38 Sudoku Solver

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

Leetcode 38 Sudoku Solver的相关文章

【leetcode】 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. 说明: 数独有

[LeetCode][JavaScript]Sudoku Solver

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 re

【leetcode】Sudoku Solver

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 re

LeetCode 037 Sudoku Solver

题目要求: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

Leetcode dfs Sudoku Solver

Sudoku Solver Total Accepted: 11799 Total Submissions: 56732My Submissions 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

leetcode:sudoku solver

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

[LeetCode] 37. Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column.

Java [leetcode 37]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. 解题思路:

LeetCode 36 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:使用暴力DFS