【LeetCode题意分析&解答】37. 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.
![]()
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【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 ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- AngularJs 常用函数
/** * [intersect 取两个数组的交集] var firstArray = [1,3,5]; var secondArray = [2,5,8]; var result */ .filte ...
- C#之多线程编程
一.进程与线程 进程(Process)是对一段静态指令序列(程序)的动态执行过程,是系统进行资源分配和调度的一个基本单位.与进程相关的信息包括进程的用户标识,正在执行的已经编译好的程序,进程程序和数据 ...
- PSP个人软件开发工具
(您的阅读是我的荣幸,如有不满之处请留言指正!) 尚未完善.....工作中 为开发人员提供一个PSP工具,简化时间记录工作:同时提供数据使用的工具,帮助开发人提高估算能力. PSP个人软件开发工具 ...
- Hadoop Eclipse远程连接出现:Error:Call to /10.10.10.10:9000 failed on local exception: java.io.EOFException
异常截图:
- 安装Ubuntu小计
因为想学Linux了,所以想装一个Linux版本尝尝鲜,听说Ubuntu桌面版很炫,所以也没有啥特定理由的选了这个版本(实际我装的时候用了Ubuntu Kylin). 具体安装过程可以参考如下的教程: ...
- mysql模糊匹配
select * from tableName where column like ""; select * from tableName where column regexp ...
- 车间任务不允许"每个装配件"超过100000
应用 Oracle Work in Progress 层 Level Function 函数名 Funcgtion Name WIP_WIPMRMDF 表单名 Form Name WIPMRMDF ...
- 限定只能处理"A仓"和"B仓"入库
应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name INV_INVTTMTX_MISC 表单名 Form Name INVTTMTX 说明 ...
- 可以把一堆dll文件放到程序目录下的一个自建目录里面
窦宁波大哥哥的那篇文章的这种写法还是很有参考价值的. QString strLibPath(QDir::toNativeSeparators(QApplication::applicationDirP ...
- Delphi中TFlowPanel实现滚动条效果
由于TFlowPanel中没有设置滚动条的相关属性.所以我们只好另辟溪径.再加一个tscrollbox来实现. 具体操作如下: 1,先添加一个Tscrollbox,设置其align为alclient. ...