LeetCode 037 Sudoku Solver
题目要求: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.
分析:
参考网址:http://www.cnblogs.com/panda_lin/archive/2013/11/04/sudoku_solver.html
回溯法尝试所有解0_o
① 对于每个空位'.',遍历1~9,check合理之后递归;
② check的时候,不用遍历整个数独,只需检查加入的行、列和块就足够了。
代码如下:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board, int x, int y) {
int row, col;
// Same value in the same column?
for (row = 0; row < 9; ++row) {
if ((x != row) && (board[row][y] == board[x][y])) {
return false;
}
}
// Same value in the same row?
for (col = 0; col < 9; ++col) {
if ((y != col) && (board[x][col] == board[x][y])) {
return false;
}
}
// Same value in the 3 * 3 block it belong to?
for (row = (x / 3) * 3; row < (x / 3 + 1) * 3; ++row) {
for (col = (y / 3) * 3; col < (y / 3 + 1) * 3; ++col) {
if ((x != row) && (y != col) && (board[row][col] == board[x][y])) {
return false;
}
}
}
return true;
}
bool internalSolveSudoku(vector<vector<char> > &board) {
for (int row = 0; row < 9; ++row) {
for (int col = 0; col < 9; ++col) {
if ('.' == board[row][col]) {
for (int i = 1; i <= 9; ++i) {
board[row][col] = '0' + i;
if (isValidSudoku(board, row, col)) {
if (internalSolveSudoku(board)) {
return true;
}
}
board[row][col] = '.';
}
return false;
}
}
}
return true;
}
void solveSudoku(vector<vector<char> > &board) {
internalSolveSudoku(board);
}
};
LeetCode 037 Sudoku Solver的更多相关文章
- Java for LeetCode 037 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- LeetCode 36 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- Java集合(类)框架(二)
1.Set集合 1.1 HashSet集合 HashSet底层为哈希码 不是数组,因此没有下标的概念,也就不能根据下标来查询某个元素 存放元素无序,不可重复 1.1.1 声明 Set<Strin ...
- Photoshop CC 习惯设置
安装后,一般设置: 1.编辑--首选项--常规 2.一般更改内容为: 性能 内存使用情况在50%-80%之间 暂存盘:除去C盘意外的其他盘 单位和标尺:以px为单位 其他根据喜好设定!
- springboot自动装配原理,写一个自己的start
springboot自动装配原理 第一次使用springboot的时候,都感觉很神奇.只要加入一个maven的依赖,写几行配置,就能注入redisTemple,rabbitmqTemple等对象. 这 ...
- c#方法 最大值我最小值
static void Main(string[] args) { int[] a = { 6, 8, 9, 5, 2, 165, 58966 }; Console.WriteLine("最 ...
- 1.python的函数参数传递
1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.append(1) fun ...
- java-Queue方法
Collection>Queue // 1. 新增 add/ offer boolean add(E e); // 队列满,IllegalStateException boolean offer ...
- linux 内核 同步原理
中断分为同步中断和异步中断. 同步中断是由CPU控制单元产生的,"同步"是指只有在一条指令执行完毕后,CPU才会发出中断,比如系统调用 异步中断是由其他硬件设备依照CPU时钟信号产 ...
- 1. 线性DP 120. 三角形最小路径和
经典问题: 120. 三角形最小路径和 https://leetcode-cn.com/problems/triangle/ func minimumTotal(triangle [][]int) ...
- shell编程之算术扩展(引号、命令替换、算术扩展)
1.单引号 .双引号.反引号的区别 单引号:忽略所有特殊字符 双引号:忽略大部分特殊字符($ `等字符除外) [root@tlinux shell]# echo '*' * [root@tlinux ...
- 慢话crush-各种crush组合
前言 ceph已经是一个比较成熟的开源的分布式存储了,从功能角度上来说,目前的功能基本能够覆盖大部分场景,而社区的工作基本上是在加入企业级的功能和易用性还有性能等方面在发力在,不管你是新手还是老手,都 ...