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. Linux 常用命令学习

    sed 大法: cat file | sed 's/string/replace_str/' file 按大小分割文件 split -b 100m filename 设置vi不自动转换tab: set ...

  2. 解决SVN:could not start external diff program的问题。

    今天装完SVN之后,用来查看文件不同老是出现could not start external diff program,网上找了很多资料也没找到自己想要的,无意中中右键 Settings看到有个Dif ...

  3. Docker的C/S模式详解

    Docker的C/S模式 Docker的C/S模式 Docker Client通过Remote API与Docker Server通信: RESTful风格API STDIN.STDOUT.STDER ...

  4. 001.web前端-学习了解

    学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的: 1.html(Hypertext Markup Language)—— ...

  5. [MUD]MUDLIB详解/MUDOS运行流程/最小MUDLIB/mud文件结构

    现在大部分中文MUD都是在东方故事(esII)基础上发展起来的,其目录结构基本一样, 也有个别MUD为了标新立异对个别目录换了个名字以示不同,但其实质没有什么变化. 这个做的最可恶的是xkx,把一个好 ...

  6. 关于Thinkphp3.2版本的分页问题

    最近公司官网改版,需要把旧的thinkphp版本换到现在最新的3.2去,因此,就开展了一系列的升级工作..在修改命名空间的同时,发现Page分页类能正常运行了,但是分页的链接却是错误的,例如在Admi ...

  7. vijos 1047 送给圣诞夜的礼品 矩阵

    题目链接 描述 当小精灵们把贺卡都书写好了之后.礼品准备部的小精灵们已经把所有的礼品都制作好了.可是由于精神消耗的缘故,他们所做的礼品的质量越来越小,也就是说越来越不让圣诞老人很满意.可是这又是没有办 ...

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

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

  9. cad画指定大小矩形

    指定基点后输入(@长度,宽度)回车 举例:如你要画个600*300的矩形 则输入@600,300回车

  10. 转 批处理 %~dp0的意义

    http://nealcai.iteye.com/blog/1685192 http://blog.csdn.net/caz28/article/details/7448677 http://stac ...