写一个程序通过填充空格来解决数独。空格用 '.' 表示。

详见:https://leetcode.com/problems/sudoku-solver/description/

class Solution {
public:
bool solveSudoku(vector<vector<char> > &board)
{
for (int i = 0; i < 9; ++i)
for (int j = 0; j < 9; ++j)
{
if ('.' == board[i][j])
{
for (int k = 1; k <= 9; ++k)
{
board[i][j] = '0' + k;
if (isValid(board, i, j) && solveSudoku(board))
return true;
board[i][j] = '.';
}
return false;
}
}
return true;
} bool isValid(vector<vector<char> > &board, int x, int y)
{
int i, j;
for (i = 0; i < 9; i++)
if (i != x && board[i][y] == board[x][y])
return false;
for (j = 0; j < 9; j++)
if (j != y && board[x][j] == board[x][y])
return false;
for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
if (i != x && j != y && board[i][j] == board[x][y])
return false;
return true;
}
};

037 Sudoku Solver 解数独的更多相关文章

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

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

  2. [LeetCode] Sudoku Solver 解数独,递归,回溯

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

  3. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  4. LeetCode 037 Sudoku Solver

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

  5. [LeetCode] Sudoku Solver 求解数独

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

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

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

  7. 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 ...

  8. LeetCode OJ:Sudoku Solver(数独游戏)

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

  9. Sudoku Solver, 求数独

    问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...

随机推荐

  1. hdu1520树形dp入门

    题目链接 题意:要开派对,邀请了上司就不能邀请他的下属,邀请了下属就不能邀请他的上司,每个人有一个值,求邀请的人的总值最大 第一行给出一个数n,代表有n个人. 下面n行分别给出n个人的的值 再下面n行 ...

  2. L97

    We are young. So let's set the world on fire. We can burn brighter than the sun.我们是青年,让我们点亮世界,释放生命,胜 ...

  3. ubuntu c++ 关机 重启 挂起 API

    #include <unistd.h> #include <linux/reboot.h> int main() { reboot(LINUX_REBOOT_MAGIC1, L ...

  4. Thrift之TProcess类体系原理及源码详细解析

    我的新浪微博:http://weibo.com/freshairbrucewoo. 欢迎大家相互交流,共同提高技术. 之前对Thrift自动生成代码的实现细节做了详细的分析,下面进行处理层的实现做详细 ...

  5. phpmyadmin数据库密码的设置

    用phpMyAdmin修改mysql数据库密码   修改mysql数据库密码方法有很多,这里向大家演示一种比较简单的方法,利用phpMyAdmin修改.   工具/原料 phpMyAdmin软件 my ...

  6. 12 Vue学习 项目技术栈

    vue2 + vuex + vue-router + webpack + ES6/7 + less + element-ui 1:vuex: Vuex 是一个专为 Vue.js 应用程序开发的状态管理 ...

  7. hibernate学习五 Hibernate补充

    1  MiddleGenIDE可以生成映射类和映射文件. 2

  8. i2c-tools的使用方法及举例

    i2c-tools的使用方法 最近在调试ADV7401,调试的过程难免要反复修改寄存器,然后看结果现象.传统的做法是修改驱动代码寄存器值->编译->下载->运行->看结果,这一 ...

  9. 三层架构与MVC比较:

    三层架构与MVC比较: 1.两者不是同一概念 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. MVC是一个设计模式,它是根据项目的具体需求来决定是否适用于该项目. 那么架构跟设计模式 ...

  10. DataTable批量插入数据库

    最近在将excel中的文件导入到数据库中,用程序进行编写,由于数据量较大所以速度很慢,后来采用了SqlBulkCopy类,解决了速度的问题,我就insert语句,sqldataadapter.upda ...