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 ...
随机推荐
- 防止 IOS 和 安卓 自动锁屏
Ios代码 在文件AppController中的 didFinishLaunchingWithOptions函数中加一行代码即可: [[UIApplication sharedApplication] ...
- react-native 解决Could not find method android() for arguments问题
运行命令行:react-native run-android 报错 Error:(23, 0) Could not find method android() for arguments [****] ...
- MLlib1.6指南笔记
MLlib1.6指南笔记 http://spark.apache.org/docs/latest/mllib-guide.html spark.mllib RDD之上的原始API spark.ml M ...
- C++ 顺序表实现
线性表就是字面上的意思, 顺序表是线性表基于数组的一种实现, “顺序”这个名字怎么来的并不清楚,可以勉强解释为“存储地址是连续.顺序的”. 另外两种线性表实现分别是“基于链表”和“散列存储”. 顺序表 ...
- RabbitMQ学习之(二)_Centos6下安装RabbitMQ及管理配置
首先yum方式安装依赖包 yum install ncurses-devel unixODBC unixODBC-devel 安装Erlang语言环境 wget http://erlang.org/d ...
- CF1155D Beautiful Array(动态规划)
做法 \(f_{i,0}\)表示以\(i\)结尾未操作时的最大值 \(f_{i,1}\)表示以\(i\)结尾正在操作时的最大值 \(f_{i,2}\)表示以\(i\)结尾已结束操作时的最大值 Code ...
- (转)国内yum源的安装(163,阿里云,epel)
国内yum源的安装(163,阿里云,epel) ----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- kali2016.2安装后配置
接触kali有几个月了,总是有一种浅尝辄止的感觉.因为不常用,一些常用操作时常想不起来了.为日后查找方便,特通过写博客方式来记录. 新建虚拟机,和安装其它操作系统差别不大,按提示一步一步安装.第1次安 ...
- Centos7.5静默安装Oracle18c
环境: CentOS7.5.Oracle18c(LINUX.X64_180000_db_home.zip) 1. 安装必要的依赖包 [root@bogon ~]# yum install bc bin ...
- Mysql MariaDB安装
1.安装 本人使用的是CentOS 7 ,默认yum安装,但默认yum安装版本有点低,可根据需要选择升级,我这里选择先升级再安装. 更新yum //更新yum包 yum -y update 配置yum ...