题目要求: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. Miniconda 安装 & Pip module 安装 & Shell 脚本调用 Miniconda 虚拟环境手册(实战项目应用)

    (实战项目应用) 1. 下载Miniconda 两个安装方式: 方式1:wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Min ...

  2. python实现非常有趣的数学问题

    1.无重复数字的三位数 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? import itertools ret = [] for i in range(1, 5) ...

  3. Spring MVC 9大组件概述

    SpringMVC中的Servlet一共有三个层次,分别是HttpServletBean.FrameworkServlet和 DispatcherServlet.HttpServletBean直接继承 ...

  4. 怎么解决Git中出现 "LF will be replaced by CRLF" 警告

    Windows中使用CRLF标识一行的结束,而在Linux/UNIX系统中只使用LF标识一行的结束.CRLF即Carriage-Return Line-Feed的缩写.通常情况下,Git库不会自动修改 ...

  5. FullCalendar v5.3.2版本制作一个航班日历Demo

    今天一个新需求是制作一个航班日历来订舱.然后我就各种找,最后找到FullCalendar,过程非常煎熬,网上例子大部分没用,大部分没有完整版.官网教程又不是很详细.搞了几天才彻底搞好这个航班日历,有需 ...

  6. leetcode97:maximum -subarray

    题目描述 请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续) 例如:给出的数组为[−2,1,−3,4,−1,2,1,−5,4], 子数组[−2,1,−3,4,−1,2 ...

  7. 利用远程桌面管理winserver集群

    在适用mstsc连接winserver服务器的场景下(别问为什么不VNC),可以利用rdp文件等方式减轻连接的操作负担 利用.rdp文件免密登录 rdp文件本质上是一个mstsc的选择,或者不如说ms ...

  8. 理解js参数

    <!DOCTYPE html><html><head> <meta charset="utf-8" /> <title> ...

  9. canvas基础[二]教你编写贝塞尔曲线工具

    贝塞尔曲线 bezierCurveTo 在线工具 https://canvature.appspot.com/ [感觉这个好用一些] https://blogs.sitepointstatic.com ...

  10. 经典c程序100例==21--30

    [程序21] 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下 的一半零一个.到第10天早 ...