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. 【转】html之file标签 --- 图片上传前预览 -- FileReader

    记得以前做网站时,曾经需要实现一个图片上传到服务器前,先预览的功能.当时用html的<input type="file"/>标签一直实现不了,最后舍弃了这个标签,使用了 ...

  2. “使用驱动器中J:的光盘之前需要将其格式化

    不知道神马原因致使U盘无法打开——大家千万注意:以后遇见这种情况千万别格式化(当然如果你的U盘或者硬盘里没有重要东西那就另当别论),进入“开始-cmd”,因为我的U盘在电脑上读出来是J盘,所以在cmd ...

  3. php 输出 sql语句

    第一种方法 $data = M('news')->field("title,date_format(postdate,'%Y-%m-%d') as postdate,content&q ...

  4. FileSystemWatcher监听文件是否有被修改

    作用:监听文件系统更改通知,并在目录或目录中的文件更改时引发事件. 需求:监听特定文件是否修改,然后做出相应的操作. 方法: ①利用一个线程,一直去查找该指定的文件是否有被修改,如果修改则操作特定步骤 ...

  5. CentOS 64位系统 yum安装32位软件包的方法

    //假如你要安装libjpeg的32位版本 1.查询具体的32位版本,然后安装 yum search libjpeg.i686 yum -y install libjpeg.i386 2.一劳永逸的方 ...

  6. MySQL优化具体

    1. 查询与索引优化分析 在优化MySQL时,通常需要对数据库进行分析,常见的分析手段有慢查询日志,profiling分析,EXPLAIN分析查询,以及show命令查询系统状态及系统变量,通过定位分析 ...

  7. 重新想,重新看——CSS3变形,过渡与动画③

    这一篇主要谈谈CSS3的过渡属性. 过渡属性被设计的十分通俗易懂,属性写法为transition,有四个子属性: <transition-property> 表示需要过渡的属性[必须](本 ...

  8. [翻译]小提示:使用figure和figcaption元素的正确方式

    figure和figcaption是一对经常被一起使用的语义化标签.如果你还没有看过规范中的定义,现在有机会在你的项目中使用它们了.如果你不知道怎么用,下面是关于如何正确使用它们的一些提示. figu ...

  9. DataStage系列教程 by Bluebreeze

    突发奇想,用了这么久的DataStage,想要写点东西祭奠那逝去的岁月.希望可以坚持一直写完. DataStage系列教程 (Change Capture) DataStage系列教程 (Pivot_ ...

  10. windchill中表格API

    表格图示 表格的测试类 package com.xiaostudy; import javax.servlet.http.HttpServletRequest; import org.apache.l ...