1. # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'
  2.  
  3. 37: Sudoku Solver
    https://oj.leetcode.com/problems/sudoku-solver/
  4.  
  5. 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.
  6.  
  7. ===Comments by Dabay===
    逐行扫描,当遇到“.”的时候,尝试每一个可能的valid_num。
    如果能DFS到底,就return True;否则,把这个位置重置为“.”,进行下一次尝试。
    '''
  8.  
  9. 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)
  10.  
  11. 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
  12.  
  13. 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))
  14.  
  15. solveSudoku2(board, (0, 0))
  16.  
  17. def print_board(board):
    print "-" * 30
    for row in board:
    for x in row:
    print "%s " % x,
    print
    print "-" * 30
  18.  
  19. 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)
  20.  
  21. if __name__ == "__main__":
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)
  22.  

[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. 将文件放到Android模拟器的SD卡中的两种解决方法

    两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...

  2. 4.1. 如何在Windows环境下开发Python

    4.1. 如何在Windows环境下开发Python 4.1. 如何在Windows环境下开发Python 4.1.1. Python的最原始的开发方式是什么样的 4.1.1.1. 找个文本编辑器,新 ...

  3. 深入理解MFC子类化

    子类化,通俗来讲就是用自己的窗口处理函数来处理特定消息,并将自己其他消息还给标准(默认)窗口处理函数.在SDK中,通过SetWindowLong来指定一个自定义窗口处理函数:SetWindowLong ...

  4. android中控件公用产生的冲突的解决办法

    1.ViewPager嵌套HorizontalScrollView滑动冲突的解决办法,重写ViewPager public class ZdyViewPage extends ViewPager { ...

  5. Cooley-Tukey算法 (蝶形算法)

    Cooley-Tukey算法差别于其它FFT算法的一个重要事实就是N的因子能够随意选取.这样也就能够使用N=rS的Radix-r算法了.最流行的算法都是以r=2或r=4为基的,最简单的DFT不须要不论 ...

  6. XML是什么,它能够做什么?——写给XML入门者

    XML就可以扩展标记语言(eXtensible Markup Language).标记是指计算机所能理解的信息符号,通过此种标记,计算机之间能够处理包括各种信息的文章等.怎样定义这些标记,既能够选择国 ...

  7. php安装配置文件 源码和yum版

    源码安装 ./configure --prefix=/usr/local/services/php \--with-config-file-path=/usr/local/service/php/et ...

  8. HTML6注册表单输入日志标题

    一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...

  9. js学习心得(一)(菜鸟)

    js基础已经打了好几次了,慕课跟着学了一遍,视频看了一些,还读了诸如 jsdom艺术,js精粹以及锋利jq(没读完). 这次再次重头读并写一遍代码,工具书是js,查缺补漏高级程序设计第二版,犀牛书有点 ...

  10. Mongodb常见错误

    1. log目录没有创建,而在logpath中有设定 2. SECONDARY默认不可以读取数据,需要db.getMongo().setSlaveOk(); 3. SECONDARY不可以写数据 4. ...