# -*- 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. HD1083 二分图,匈牙利算法

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stdio.h> #include<stdlib. ...

  2. 诺基亚 Lumia 1020的价格

       大部分的中国人心中,都有一个诺基亚情节.经典的1110,耐摔程度强得惊人,相信很多人都知道它的厉害.    虽说这些年诺基亚由于没有跟上Android这一波智能机的浪潮,在智能机时代被三星苹果等 ...

  3. Amzon MWS API开发之 上传数据

    亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...

  4. MySQL 5.6 中 TIMESTAMP 的变化

    http://www.williamsang.com/archives/818.html

  5. mysql数据损坏修复方法

    1.myisamchk使用 myisamchk 必须暂时停止 MySQL 服务器.例如,我们要检修 discuz 数据库.执行以下操作:# service mysql stop (停止 MySQL ) ...

  6. Python中:self和__init__的含义 + 为何要有self和__init__

    Python中:self和__init__的含义 + 为何要有self和__init__ 背景 回复: 我写的一些Python教程,需要的可以看看 中SongShouJiong的提问: Python中 ...

  7. popen3

    windows版本popen3函数 整理了下10年08月给 fossil 捐的代码  implementation of function “popen2” on win32——参考自 Creatin ...

  8. [转载]CTO和技术总监区别

    原文地址:http://blog.sina.com.cn/s/blog_6024cfa90101cb0h.html 技术总监(Chief Technical Officer)与CTO(Chief Te ...

  9. 【LeetCode练习题】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  10. Aix5~6小机运维

    1,0516-787 extendlv: Maximum allocation for logical volume hd3        is 512 smitt chlv改max logical ...