LeetCode OJ: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.
dfs,一直寻找,不行返回即可:
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
doSudoku(board);
} bool checkValid(vector<vector<char>>&board, int x, int y)
{
for(int i = ; i < ; i++){
if(i!=x)
if(board[i][y] == board[x][y]) return false;
} for(int j = ; j < ; j++){
if(j != y)
if(board[x][j] == board[x][y]) return false;
} for(int i = (x/) * ; i < (x/ + ) * ; ++i){
for(int j = (y/) * ; j < (y/ + ) * ; ++j){
if((i!=x) || (j != y))
if(board[x][y] == board[i][j]) return false;
}
}
return true;
} bool doSudoku(vector<vector<char>> & board)
{
for(int row = ; row < ; ++row){
for(int col = ; col < ; ++col){
if(board[row][col] == '.'){
for(int i = ; i <= ; ++i){
board[row][col] = '' + i;
if(checkValid(board, row, col)){
if(doSudoku(board)){
return true;
}
}
board[row][col] = '.';
}
return false;
}
}
}
return true;
}
};
LeetCode OJ:Sudoku Solver(数独游戏)的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Leetcode0037--Sudoku Solver 数独游戏
[转载请注明]http://www.cnblogs.com/igoslly/p/8719622.html 来看一下题目: Write a program to solve a Sudoku puzzl ...
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- [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 解数独
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 : 解决数独问题,给出一个二维数组,将这个数独 ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [Leetcode] valid sudoku 有效数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- Java线程的几个概念
线程的生命周期: 新建状态:用new语句创建的线程对象处于新建状态,此时它和其它的java对象一样,仅仅在堆中被分配了内存 就绪状态:当一个线程创建了以后,其他的线程调用了它的start()方法,该线 ...
- Ubuntu14.04+caffe+cuda7.5 环境搭建以及MNIST数据集的训练与测试
Ubuntu14.04+caffe+cuda 环境搭建以及MNIST数据集的训练与测试 一.ubuntu14.04的安装: ubuntu的安装是一件十分简单的事情,这里给出一个参考教程: http:/ ...
- QML中的state 状态
QML中的状态其实很好理解,任何事物在某一事件都是有一个状态的. 比如你看到的一个窗口,这个时候里面的文字和图片正处于某个状态中.比如一个超链接,你点击了,发现颜色变了,你按了Ctrl+A,整个窗体好 ...
- oracle中修改表已有数据的某一列的字段类型的方法,数据备份
1.在开发过程中经常会遇到表中的某一个字段数据类型不对,比如说需要保存的数据带小数,但是在最初设计的时候是给的number(10)类型,开始保存是整数的时候满足要求,后来在保存小数的时候 会发现自动四 ...
- vi重要操作指令
[Ctrl] + [f] 萤幕『向下』移动一页,相当于[Page Down]按键( 常用 ) [Ctrl] + [b] 萤幕『向上』移动一页,相当于[Page Up]按键( 常用 ) 0 或功能键[H ...
- hadoop HA + kerberos HA集群搭建问题和测试总结
1. 常见问题 (1)hostname设置问题.vi /etc/sysconfig/network (2)集群/etc/hosts没有统一. (3)yarn slave需要单独启动../sbin/y ...
- 20145310 《Java程序设计》第5周学习总结
20145310 <Java程序设计>第5周学习总结 教材学习内容总结 本周主要进行第八章和第九章的学习. java中所有的错误都会打包为对象,可以try catch代表错误的对象后做一些 ...
- KALI视频学习11-15
KALI视频学习11-15 第十一集 看到openvas的主界面(web界面) ping靶机,看是否能正常连通 创建一个扫描目标Configuration-Targets,默认扫描目标为本机 添加一个 ...
- Qt+VS编译出错:Two or more files with the name of moc_Geometry.cpp will produce outputs to the same location
warning MSB8027: Two or more files with the name of moc_Geometry.cpp will produce outputs to the sam ...
- 快用Visual Studio(三)- 代码重构
什么是代码重构 编写代码 | 找到代码 | 修改代码 关于重构的工具 Bracket Matching Selection Cursors Intelligence Parameter hints E ...