递归和回溯_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

转载于:https://www.cnblogs.com/lux-ace/p/10556925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值