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 ...
随机推荐
- TOSCA自动化测试工具--TestSuite Components
TestSuite Components 分了6块
- playbook实现nginx安装
1. 先在一台机器上编译安装好nginx,然后打包 tar -zcvf nginx.tar.gz /usr/local/nginx --exclude=conf/nginx.conf --exclud ...
- Ubuntu16.04中查看硬盘的型号和读取速度
最近在测试FTP服务器,上传和下载的速度与很多因数有关,其中,硬盘的读取速度就是其中不同的区别点,我同时用了三台不用的服务器架设FTP服务,一台是出来ftp服务外还含平台其他管理软件,一台是全新的系统 ...
- 【Error】安装程序无法打开注册表项 UNKNOWN\Components\...
在安装程序的时候出现错误信息: 解决方法: 依次点击开始,所有程序,附件,右键单击命令提示符,选择以管理员身份运行.运行secedit /configure /cfg %windir%\inf\def ...
- OpenStack、KVM、VMWare和Docker
一.虚拟化 1.什么是虚拟化 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内 ...
- 20145310 GDB调试汇编堆栈分析
GDB调试汇编堆栈分析 由于老师说要逐条分析汇编代码,所以我学习卢肖明同学的方法,重新写了一篇博客. 代码: #include<stdio.h> short addend1 = 1; st ...
- Centos下ftp协议连接远程ftp server主机
环境说明 [root@Check3 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@Check3 ~]# uname -a L ...
- [翻译]小提示:使用figure和figcaption元素的正确方式
figure和figcaption是一对经常被一起使用的语义化标签.如果你还没有看过规范中的定义,现在有机会在你的项目中使用它们了.如果你不知道怎么用,下面是关于如何正确使用它们的一些提示. figu ...
- Redis维护
关闭redis [root@--x-x log]# cd /usr/local/redis/bin/ [root@--x-x bin]# ./redis-cli -h shutdown 启动redis ...
- 【论文解析】MTCNN论文要点翻译
目录 0.论文连接 1.前言 2.论文Abstract翻译 3.论文的主要贡献 4.4 训练 5 模型性能分析 5.1 关于在线挖掘困难样本的性能 5.2 将人脸检测与对齐联合的性能 5.3 人脸检测 ...