题目:

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. Android学习小Demo(20)关于Fragment的应用

    Android在3.0之后引入了Fragment的概念,我推測其想法可能仅仅是想更好地兼容大屏幕或者平板的开发,由于大屏幕能够展示很多其它的内容,而内容一多,逻辑有可能就乱,而利用Fragment,则 ...

  2. PS 过滤器——运动模糊

    %%%%%  motion blur clc; clear all; close all; Image=imread('4.jpg'); Image=double(Image); theta=pi/4 ...

  3. NSIS:简单按钮美化插件SkinButton,支持透明PNG图片。

    原文 NSIS:简单按钮美化插件SkinButton,支持透明PNG图片. 征得作者贾可的同意,特发布按钮美化插件SkinButton. 插件说明: 使用GDI+库写的一个简单按钮美化插件,支持透明P ...

  4. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  5. Swift的74标准功能

    Swift中共同拥有74个内建函数,可是在Swift官方文档("The Swift Programming Language")中仅仅记录了7中.剩下的67个都没有记录. 本文将列 ...

  6. net网站运行在自定义的Web服务器上

    ASP.NET 开发必备知识点(1):如何让Asp.net网站运行在自定义的Web服务器上   一.前言 大家都知道,在之前,我们Asp.net 的网站都只能部署在IIS上,并且IIS也只存在于Win ...

  7. SQL Server 备份和还原

    SQL Server 备份和还原   SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...

  8. Android 滑动界面实现---Scroller类别 从源代码和开发文档了解(让你的移动布局)

    在android学习,行动互动是软件的重要组成部分,其中Scroller是提供了拖动效果的类,在网上.比方说一些Launcher实现滑屏都能够通过这个类去实现.. 样例相关博文:Android 仿 窗 ...

  9. 网络资源(4) - extJS视频

    2014_08_24 http://v.youku.com/v_show/id_XMjk2ODc0MjA4.html?f=7183617 extJS视频教程04——ExtJS框架入门

  10. Tyvj P1015 公路骑 (DP)

     叙述性说明 Description 一个特殊的单行道都在每公里公交车站.们乘坐汽车的公里使来付费. 比如例子的第一行就是一个费用的单子. 没有一辆车子行驶超过10公里.一个顾客打算行驶n公里(1 ...