码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Sudoku Solver python

时间:2015-12-20 00:38:49      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

#

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.



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

 

leetcode Sudoku Solver python

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5060066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!