Leetcode: Sudoku Solver

Question

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.

Solution

Get idea from here.

Actually, it is an easy problem.

class Solution(object):
    def solveSudoku(self, board):
        """ :type board: List[List[str]] :rtype: void Do not return anything, modify board in-place instead. """

        if board==None or len(board)!=9 or len(board[0])!=9:
            return
        self.helper(board,0,0)

    def helper(self, board, i, j):
        if j>=9:
            return self.helper(board, i+1, 0)
        if i==9:
            return True

        if board[i][j]=='.':
            for elem in range(1,10):
                board[i][j] = str(elem)
                if self.isValid(board,i,j):
                    if self.helper(board, i,j+1):
                        return True
                board[i][j]='.'
        else:
            return self.helper(board, i, j+1)

        return False

    def isValid(self, board, i, j):
        for k in range(9):
            if k!=j and board[i][k]==board[i][j]:
                return False

        for k in range(9):
            if k!=i and board[k][j]==board[i][j]:
                return False

        for row in range(i/3*3, i/3*3+3):
            for col in range(j/3*3, j/3*3+3):
                if (row!=i or col!=j) and board[row][col]==board[i][j]:
                    return False

        return True

你可能感兴趣的:(Leetcode: Sudoku Solver)