题目:

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。一直遍历找到符合的解,这个过程有相应的剪枝,剪就通过下面的isValid来进行。需要注意的地方已经给了注释。诉说千字不如代码一行。

class Solution {
private:
bool isValid(vector<vector<char> > &board, int x, int y)
{
for (int i = 0; i < 9; ++i) // 判断行是否有重复字符
{
if (board[i][y] == board[x][y] && i != x)
return false;
}
for (int i = 0; i < 9; ++i) // 列
{
if (board[x][i] == board[x][y] && i != y)
return false;
}
for (int i = 3*(x/3); i < 3*(x/3)+3; ++i) // 子九宫是否有重复
for (int j = 3*(y/3); j < 3*(y/3)+3; ++j) // 纸上画一个框就能推出其中每个格子的起点和终点与xy的关系
{
if (board[i][j] == board[x][y] && i != x && j != y)
return false;
}
return true; // 都没有就返回合法
}
public:
bool solveSudoku(vector<vector<char> > &board)
{
// backtracking
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) // 注意了是1到9,别误以为0到8
{
board[i][j] = '0'+ k; // = 别写成 == 了,调式了十分钟发现我写成==,泪崩
if (isValid(board, i, j) && solveSudoku(board)) // 如果加入的数合法,并且,加入后的有解,那就true
return true;
board[i][j] = '.'; // if没有返回那就是k取值不合法,所以要重新把元素恢复为'.'
}
return false;
}
}
return true; // 最后一次所有的都填了数字了,解成功了就返回正确
}
};

leetcode第36题--Sudoku Solver的更多相关文章

  1. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  2. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  3. LeetCode(37) Sudoku Solver

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

  4. LeetCode第[36]题(Java):Valid Sudoku

    题目:有效的数独表 难度:Medium 题目内容: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be ...

  5. 【一天一道LeetCode】#36. Valid Sudoku

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...

  6. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

  7. 【LeetCode】36 - Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...

  8. 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)

    Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...

  9. [Leetcode 37]*数独游戏 Sudoku Solver 附解释

    [题目] 每一行.每一列.每个3*3的格子里只能出现一次1~9. [思路] 参考了思路,附加了解释. dfs遍历所有非空格子,n是已经填好的个数. 初始化条件.n=81,都填了,返回结束.对于已经填好 ...

随机推荐

  1. mapxtreme C# 完美车辆动态轨迹展示

    演示程序请在 http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找 Trajectory.rar 轨迹回放功 ...

  2. vs2015管理github代码

  3. 走向DBA[MSSQL篇] 详解游标

    原文:走向DBA[MSSQL篇] 详解游标 前篇回顾:上一篇虫子介绍了一些不常用的数据过滤方式,本篇详细介绍下游标. 概念 简单点说游标的作用就是存储一个结果集,并根据语法将这个结果集的数据逐条处理. ...

  4. 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)

    http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...

  5. Gradle digest

    task类型 copy task copyFiles(type: Copy) { from 'resources' into 'target' include '**/*.xml', '**/*.tx ...

  6. Could not roll back JDBC transaction途径

    [异常]接口数量:DM02;错误代码:ERR_EAI_02_014; 错误叙述性说明:当将中间库异常Could not roll back JDBC transaction; nested excep ...

  7. ORA-00838: Specified value of MEMORY_TARGET is too small(转)

    1. 测试环境OS: RHEL5U5(32bit)DB: Oracle 11.2.0.3.0(32bit) 2.   异常原因. 2.1 oracle 11g默认sga_target为0,如下图, O ...

  8. Memcache功能具体解释

    memcache函数全部的方法列表例如以下: Memcache::add – 加入一个值.假设已经存在,则返回false Memcache::addServer – 加入一个可供使用的server地址 ...

  9. JS日期时间选择器

    本文介绍一种日期和时间选择器的使用方法.此选择器由jqueryUI实现,支持精确到毫秒的时间选择. 此选择器项目地址为http://trentrichardson.com/examples/timep ...

  10. User、Role、Permission数据库设计ABP

    ABP 初探 之User.Role.Permission数据库设计 (EntityFramework 继承的另一种使用方法) 最近群里(134710707)的朋友都在讨论ABP源码,我把最近学习的内容 ...