题目要求: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.

分析:

参考网址:http://www.cnblogs.com/panda_lin/archive/2013/11/04/sudoku_solver.html

回溯法尝试所有解0_o

① 对于每个空位'.',遍历1~9,check合理之后递归;

② check的时候,不用遍历整个数独,只需检查加入的行、列和块就足够了。

代码如下:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board, int x, int y) {
int row, col; // Same value in the same column?
for (row = 0; row < 9; ++row) {
if ((x != row) && (board[row][y] == board[x][y])) {
return false;
}
} // Same value in the same row?
for (col = 0; col < 9; ++col) {
if ((y != col) && (board[x][col] == board[x][y])) {
return false;
}
} // Same value in the 3 * 3 block it belong to?
for (row = (x / 3) * 3; row < (x / 3 + 1) * 3; ++row) {
for (col = (y / 3) * 3; col < (y / 3 + 1) * 3; ++col) {
if ((x != row) && (y != col) && (board[row][col] == board[x][y])) {
return false;
}
}
} return true;
} bool internalSolveSudoku(vector<vector<char> > &board) {
for (int row = 0; row < 9; ++row) {
for (int col = 0; col < 9; ++col) {
if ('.' == board[row][col]) {
for (int i = 1; i <= 9; ++i) {
board[row][col] = '0' + i; if (isValidSudoku(board, row, col)) {
if (internalSolveSudoku(board)) {
return true;
}
} board[row][col] = '.';
} return false;
}
}
} return true;
} void solveSudoku(vector<vector<char> > &board) {
internalSolveSudoku(board);
}
};

LeetCode 037 Sudoku Solver的更多相关文章

  1. Java for LeetCode 037 Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【leetcode】Sudoku Solver

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

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

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

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

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

  5. Java [leetcode 37]Sudoku Solver

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

  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(求解数独)

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

  8. 037 Sudoku Solver 解数独

    写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...

  9. LeetCode 36 Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

随机推荐

  1. Java学习的第二十六天

    1.过滤处理流 DataOutputStream输入数据 用DataInputStream读数据 2.方法太多记不清 3.明天学习内存操作流和缓冲流

  2. 数据库本地data source

    数据源表示数据库所在设备的ip地址或名称,基本上有以下几种写方法 data source = (local) data source = "127.0.0.1" data sour ...

  3. Route53导出解析记录

    使用AWS CLI来进行导出 可以参考AWS的官方文档 安装AWS CLI 官方安装aws cli文档 curl "https://awscli.amazonaws.com/awscli-e ...

  4. 【Luogu】P1436 棋盘分割 题解

    嗯,点开题目,哇!是一道闪亮亮的蓝题! 不要被吓到了,其实,这道题就是一个简单的DP啦! 我们设 \(f[x1][y1][x2][y2][c]\) 为以 \((x1,y1)\) 为左上角,以 \((x ...

  5. ()C++中的赋值运算符重载函数(operator=)

    本文主要介绍C++中的重载操作符(operator)的相关知识. 概述 1.1 what operator 是C++的一个关键字,它和运算符(如=)一起使用,表示一个运算符重载函数,在理解时可将ope ...

  6. C++实现RTMP协议发送H.264编码及AAC编码的直播软件开发音视频

    RTMP(Real Time Messaging Protocol)是专门用来传输音视频数据的流媒体协议,最初由Macromedia 公司创建,后来归Adobe公司所有,是一种私有协议,主要用来联系F ...

  7. CO函数库

    CO函数库 用于 Generator 函数的自动执行,co函数返回一个promise对象 Generator 函数: 协程的概念:A执行- A暂停,执行权给B--- B交回执行权--- A恢复执行 G ...

  8. 基于gin的golang web开发:mysql增删改查

    Go语言访问mysql数据库需要用到标准库database/sql和mysql的驱动.标准库的Api使用比较繁琐这里再引入另一个库github.com/jmoiron/sqlx. go get git ...

  9. tp3.2自动验证

    <?php namespace Home\Model; use Think\Model; class UserModel extends Model{ protected $patchValid ...

  10. 1,github更新问题

    赶去吃饭,不排版了. 一:上传 1,新建仓库 通过下面代码克隆仓库到本地: git clone https://github.com/ndnmonkey/zhihu.git 把文件放到克隆到本地的文件 ...