# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solver
https://oj.leetcode.com/problems/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. ===Comments by Dabay===
逐行扫描,当遇到“.”的时候,尝试每一个可能的valid_num。
如果能DFS到底,就return True;否则,把这个位置重置为“.”,进行下一次尝试。
''' class Solution:
# @param board, a 9x9 2D array
# Solve the Sudoku by modifying the input board in-place.
# Do not return any value.
def solveSudoku(self, board):
def next_position(position):
i, j = position
j += 1
if j >= 9:
j -= 9
i += 1
return (i, j) def valid_nums(board, position):
i, j = position
s = [str(n) for n in xrange(1, 10)]
for row in xrange(9):
if board[row][j] != '.' and board[row][j] in s:
s.remove(board[row][j])
for col in xrange(9):
if board[i][col] != '.' and board[i][col] in s:
s.remove(board[i][col])
ii = i / 3
jj = j / 3
for row in xrange(3):
for col in xrange(3):
if board[ii*3+row][jj*3+col] != '.' and board[ii*3+row][jj*3+col] in s:
s.remove(board[ii*3+row][jj*3+col])
return s def solveSudoku2(board, position):
i, j = position
if i == 9:
return True
if board[i][j] == '.':
nums = valid_nums(board, position)
for n in nums:
board[i][j] = n
if solveSudoku2(board, next_position(position)) is True:
return True
board[i][j] = '.'
else:
return solveSudoku2(board, next_position(position)) solveSudoku2(board, (0, 0)) def print_board(board):
print "-" * 30
for row in board:
for x in row:
print "%s " % x,
print
print "-" * 30 def main():
s = Solution()
board = [
["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"]
]
print_board(board)
s.solveSudoku(board)
print_board(board) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]37: Sudoku Solver的更多相关文章

  1. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  2. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  3. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  4. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  5. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  6. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  9. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

随机推荐

  1. ctype.h库函数

    头文件ctype.h声明了一组用于分类和转换单个字符的函数.所有的函数都接收一个int型的参数,并返回一个int——返回的int可能代表一个字符,也可能代表的是bool值(0为假,非0为真). 你可能 ...

  2. UVA 1572 Self-Assembly

    拓扑排序,以边上标号为点,正方形为边,拓扑图中存在有向环时unbounded,否则bounded: 注意:仔细处理输入:   遍历一个点时,下一次遍历拼上的下一个方形边:即假设遍历到 A+ 时,下次从 ...

  3. android layout的布局

    1.android:layout_width.android:layout_heigth 表示控件的大小,长宽 2.andoid:layout_gravity .android:gravity表示控件 ...

  4. window.opener方法的使用 js跨域

    原文:window.opener方法的使用 js跨域 最近公司网站登陆加入了第三方登陆.可以用QQ直接登陆到我们网站,在login页面A中点QQ登陆时,调用了一个window.open文件打开一个lo ...

  5. JavaScript实现私有属性

    原文:JavaScript实现私有属性 JavaScript被很多人认为并不是一种面向对象语言,原因有很多种,比如JavaScript没有类,不能提供传统的类式继承:再比如JavaScript不能实现 ...

  6. Inno Setup 打包工具总结(转)

    最近打包用到了Inno setup,在这个过程中容易犯一些低级错误,特别写出来已提醒自己 1.打包文件夹 打包文件按照向导来一般没什么问题,但文件夹就不一样了.向导生成的打包文件夹的代码如下: Sou ...

  7. cf459B Pashmak and Flowers

    B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. 安装Discuz!论坛时提示“mysqli_connect() 不支持 advice_mysqli_connect”

    安装Discuz!论坛时提示“不支持Mysql数据库,无法安装论坛”的解决方法1,在系统的 system32(C:\windows\system32)目录下缺少libmysql.dll文件,解决方法是 ...

  9. CSS3 过渡

    通过 CSS3,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 请把鼠标移动到右侧的元素上: 浏览器支持 Internet E ...

  10. 05JS高级 方法没有块级作用域

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...