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.

题意分析:

  本题是要解决一个数独问题。输入一个部分数独,将空白的格子填上正确的数字,只需要找到一个正确的解即可。

解答:

  关于数独的规则,在上一篇文章里面有讲【LeetCode题意分析&解答】36. Valid Sudoku,这里不再赘述。

  填入的数字是在1~9之间,我们先将当前节点置为某个要填入的数,并判断是否符合数独的规则。如果符合则继续循环,如果不符合则退回来填入其他的数。如果按照这个思路去做,就是一个典型的深度优先搜索的回溯算法。

AC代码:

class Solution(object):
def solveSudoku(self, board):
def backtracing():
for i in xrange(9):
for j in xrange(9):
if board[i][j] != '.':
continue
for v in [str(c) for c in xrange(1, 10)]:
# judge if against to the rule
if v in board[i] or v in [board[k][j] for k in xrange(9)] or v in \
[board[i / 3 * 3 + m][j / 3 * 3 + n] for m in xrange(3) for n in xrange(3)]:
continue
board[i][j] = v
if backtracing():
return True
else:
board[i][j] = '.'
return False
return True
backtracing()

【LeetCode题意分析&解答】37. Sudoku Solver的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  3. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  6. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. MSSQL查询连接数

    SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FROM [Master].[dbo].[SYS ...

  2. oracle tablespace

    oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...

  3. Eclipse 浏览文件插件- OpenExplorer

    http://blog.csdn.net/w709854369/article/details/6599167 EasyExplorer  是一个类似于 Windows Explorer的Eclips ...

  4. JqMobi学习

    JqMobi+phonegap+html5 开发Android.ios应用

  5. JVM学习之强引用、弱引用、软引用、虚引用

    转自:http://my.oschina.net/ydsakyclguozi/blog/404389 多谢博主分享 1.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象 ...

  6. ubuntu中Mysql常用命令整理

    启动mysql服务sudo /etc/init.d/mysql start 关闭mysql服务sudo /etc/init.d/mysql stop

  7. keyDown keyPress keyUp 事件的区别

    keyDown keyPress keyUp  事件的区别 一 触发顺序 显而易见,事件发生的顺序是: keydown --> keypress --> keyup 当按住一个键一段时间后 ...

  8. classic asp中使用ADODB.Command防止sql injection

    原始代码如下 Set Conn = Server.CreateObject("Adodb.Connection") Conn.Open "Provider=Microso ...

  9. 1.9 需求订单导入MDS

    1.9          需求订单导入MDS 1.9.1   业务方案描述 将”需求订单维护表”中完成调整维护的需求订单导入系统标准MDS中,使之驱动对应的物料需求计划(MRP)的运行. 1.9.2  ...

  10. 第五章 Spring3.0 、Hibernate3.3与Struts2的整合 基于Annotation

    Annotation的方式是通过注解的方式把Struts2中的Action.Dao层的实现类.Service层的实现类交由Spring管理,不需要在配置文件中进行配置.但为了方便,事务的管理依然使用的 ...