[Leetcode][Python]37: Sudoku Solver
# -*- 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 "-" * 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的更多相关文章
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
随机推荐
- ZJUTACM(hd1259)
ZJUTACM 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【测试环境】cywin的简单介绍
有的时候,单位可能不会这么慷慨给你很多硬件设备供你在任何环境下面都能够工作,但我们有时候需要unix环境,这个时候cywin诞生了... 该工具非常强大,基本上能够满足您的基本需求: 1.安装cywi ...
- Oracle EBS-SQL (MRP-5):重起MRP Manager.sql
UPDATE fnd_concurrent_processes SET process_status_code = 'K' WHERE process_status_code not in ('K', ...
- UI组件
1.自定义View 2.布局管理器-----ViewGroup 3.textview及其子类 4.imageview及其子类 5.adapterview及其子类----ViewGroup 6.prog ...
- Mysql ORM工具--MicrobeORM.Mysql开源咯
MicrobeORM.Mysql 补充:为啥批批量比官方的ADO.NET还快,原因是这俩货 System.EnterpriseServices.dllSystem.EnterpriseServices ...
- CSS 注意事项
使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读. 明确定义单位,除非值为0 忘记定义尺寸的单位是CSS新手普遍的错误.在HTML中你可以只写width="100&q ...
- 2014.8.15模拟赛【公主的工作】&&bzoj1046[HAOI2007]上升序列
bzoj题目是这样的 Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm ...
- NODE.JS安装配置
- sysbench的安装与使用
sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL 安装过程(rhel5.8+mys ...
- IOS5开发-http get/post调用mvc4 webapi互操作(图片上传)[转]
IOS5开发-http get/post调用mvc4 webapi互操作(图片上传) 目前最流行的跨平台交互是采用http协议通过JSON对象进行互操作.这种方式最简单,也很高效.webservi ...