zoukankan      html  css  js  c++  java
  • 递归和回溯_leetcode37

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


    self.m = len(board)
    self.n = len(board[0])

    self.visit = [[False for i in range(self.n)] for i in range(self.m)]

    # wrong
    self.direction = [[-1,0],[0,1],[1,0],[0,-1]]

    self.count = 0

    self.startX = None
    self.startY = None

    for x in range(self.m):
    for y in range(self.n):
    if board[x][y] == ".":
    self.count += 1
    if not self.startX and not self.startY :
    self.startX = x
    self.startY = y


    self.putNumber(board,self.startX,self.startY)



    def inArea(self,x,y):
    return x >= 0 and x < self.m and y >= 0 and y < self.n


    def putNumber(self,board,x,y):


    self.visit[x][y] = True

    for i in range(1,10):

    if self.validRow(board,x,i) and self.validCol(board,y,i) and self.validSquare(board,x,y,i):

    board[x][y] = str(i)
    self.count -= 1

    if self.count == 0:
    print board

    for item in self.direction:
    newX = x + item[0]
    newY = y + item[1]

    if self.inArea(newX,newY) and not self.visit[newX][newY] and board[newX][newY] == ".":

    self.putNumber(board,newX,newY)


    board[x][y] = "."
    self.count -= 1


    self.visit[x][y] = False
    return


    def validRow(self,board,x,num):

    for i in range(self.n):
    if board[x][i] == str(num):
    return False
    return True


    def validCol(self,board,y,num):
    for i in range(self.m):
    if board[i][y] == str(num):
    return False
    return True

    def validSquare(self,board,x,y,num):

    xIndex = x / 3
    yIndex = y /3

    sX = xIndex * 3
    sY = yIndex * 3

    for i in range(3):
    for j in range(3):
    if board[sX+i][sY+j] == str(num):
    return False
    else:
    return True


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

    self.m = len(board) self.n = len(board[0]) self.res = [] self.count = 0 pointSet = [] for x in range(self.m): for y in range(self.n): if board[x][y] == ".": pointSet.append([x,y]) self.putNumber(board,pointSet,0) def inArea(self,x,y): return x >= 0 and x < self.m and y >= 0 and y < self.n def putNumber(self,board,pointSet,index): if index == len(pointSet): for i in range(self.m): self.res.append(board[i][0:]) return True x = pointSet[index][0] y = pointSet[index][1] for i in range(1,10): if self.validRow(board,x,i) and self.validCol(board,y,i) and self.validSquare(board,x,y,i): board[x][y] = str(i) if self.putNumber(board,pointSet,index+1): return board[x][y] = "." return def validRow(self,board,x,num): for i in range(self.n): if board[x][i] == str(num): return False return True def validCol(self,board,y,num): for i in range(self.m): if board[i][y] == str(num): return False return True def validSquare(self,board,x,y,num): xIndex = x / 3 yIndex = y /3 sX = xIndex * 3 sY = yIndex * 3 for i in range(3): for j in range(3): if board[sX+i][sY+j] == str(num): return False else: return Trues = Solution2()# board1 = [# ["5","3","."],# ["6",".","."],# [".","9","8"]# ]board2 = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]s.solveSudoku(board2)print s.res
  • 相关阅读:
    数位dp模板
    HDU
    hdu 2586 How far away ? ( 离线 LCA , tarjan )
    POJ 1655 Balancing Act( 树的重心 )
    HDU 2196 Computer( 树上节点的最远距离 )
    HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
    HDU 5265 pog loves szh II
    2015区域赛起航
    2015GDCPC广东省赛总结
    SGU 521 North-East ( 二维LIS 线段树优化 )
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10556925.html
Copyright © 2011-2022 走看看