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(数独游戏)的更多相关文章

  1. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  2. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  3. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  4. Leetcode0037--Sudoku Solver 数独游戏

    [转载请注明]http://www.cnblogs.com/igoslly/p/8719622.html 来看一下题目: Write a program to solve a Sudoku puzzl ...

  5. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

  6. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  7. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  8. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  9. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  10. [Leetcode] valid sudoku 有效数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

随机推荐

  1. ajax的认识

    1. ajax是一种技术,无需刷新页面即可向服务器传输.读写数据. 2. ajax的参数说明: 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为 ...

  2. 【c++ primer, 5e】构造函数 & 拷贝、赋值和析构

    [构造函数] 1.构造器就是创建对象时被调用的代码. 2.如果没有自定义构造器,那么编译器将自动合成一个默认的无参构造器. 3.自定义的构造器不允许加const,所创建const的对象只有在构造器代码 ...

  3. Swoole学习(六)Swoole之定时器的创建与清除

    环境:Centos6.4,PHP环境:PHP7,Swoole2.1(在指定的时间后执行函数,需要1.7.7或更高版本) <?php //----------------------------- ...

  4. Asp.net Core Windows部署

    一.  IIS 部署模式 1. 安装IIS服务 2. 下载安装Core SDK        https://www.microsoft.com/net/download/Windows/build3 ...

  5. 在VMware中使用Nat方式设置静态IP

    为了在公司和家中不改变ip,所以采用vm的NAT模式来设置静态ip 1.vm采用NAT模式联网 2.编辑vm虚拟机设置 3.查看该网段的网关 可以看出网关为192.168.44.2,然后开始设置静态i ...

  6. 20145313张雪纯 《Java程序设计》第5周学习总结

    20145313张雪纯 <Java程序设计>第5周学习总结 教材学习内容总结 JAVA中所有错误都会被打包成对象,可以用尝试(try)捕捉(catch)代表错误的对象后做一些处理.使用tr ...

  7. cogs 547:[HAOI2011] 防线修建

    ★★★☆   输入文件:defense.in   输出文件:defense.out   简单对比 时间限制:1 s   内存限制:128 MB 题目描述: 近来A国和B国的矛盾激化,为了预防不测,A国 ...

  8. CentOS 7配置静态IP地址

    [root@centos1 ~]# ifconfig -bash: ifconfig: command not found 首先,习惯性的输入echo $PATH(查看当前PATH环境变量,跟DOS的 ...

  9. JavaConfig 使用Java代码进行显示配置

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. 需要先加载spring-context 包 &l ...

  10. 多线程资源隔离之ThreadLocal

    上篇讲到多线程线程安全问题的解决思路,这篇将详细讲解资源隔离ThreadLocal的实践. ThreadLocal也叫线程局部变量,类似Map结构,以当前线程为key.既然是以资源隔离的思想保证线程安 ...