该博客好好分析

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.

回溯法的思想!!(剪枝+回溯+递归运用)

分析:

首先遍历整个九宫格,并进行标记!

rowValid[row][val]等于1表示:row行val已经存在(定义域,row从0~8,val从1~9)

colValid[col][val]等于1表示:col列val已经存在

subGrid[row/3*3+col/3][val]等于1表示:第w/3*3+col/3个子九宫格中val已经存在

利用index来进行算法搜索控制!数独一共有81个数字,0~80;因此当index>80时,算法有解并结束。

 class Solution {
public:
void solveSudoku(vector<vector<char>>& board)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
{
if(board[i][j]!='.')
handle(i,j,board[i][j]-'');
}
solve(board,);
} bool solve(vector<vector<char>>& board,int index)
{
if(index>)
return true;
int row=index/;
int col=index-index/*;
if(board[row][col]!='.')
return solve(board,index+);
for(int num='';num<='';num++)//int num='1';num<'10';num++,这一句的问题所在,'10'是字符串呢还是字符呢? //每个为填充的格子有9种可能的填充数字
{
if(isValid(row,col,num-''))
{
board[row][col]=num;
handle(row,col,num-'');
if(solve(board,index+)) return true;
reset(row,col,num-'');
}
}
board[row][col]='.';
return false;
}
bool isValid(int row,int col,int val)
{
if(rowValid[row][val]== && colValid[col][val]== && subGrid[row/*+col/][val]==)
return true;
return false;
}
void handle(int row,int col,int val)
{
rowValid[row][val]=;
colValid[col][val]=;
subGrid[row/*+col/][val]=;
}
void reset(int row,int col,int val)
{
rowValid[row][val]=;
colValid[col][val]=;
subGrid[row/*+col/][val]=;
} private:
int rowValid[][];
int colValid[][];
int subGrid[][];
};

Sudoku Solver Backtracking的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

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

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

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

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

  5. 【leetcode】Sudoku Solver

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

  6. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  7. 【LeetCode】37. Sudoku Solver

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

  8. Valid Sudoku&&Sudoku Solver

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

  9. LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver

    1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...

随机推荐

  1. (整理)SQL server 2012 中文乱码与5030错误

    安装2012后,一直没注意到中文的问题.直到有一天,突然发现字段内容竟然是“??”,然后一通查,原来是排序规则需要改变: 选择数据库->右键属性->选项,将排序规则改成“Chinese_P ...

  2. java后台异步任务执行器TaskManager

    java后台异步任务执行器TaskManager 此方式基于MVC方式: 一,使用任务: @Resource private TaskManager taskManager; public strin ...

  3. 如何将arcgis的mxd文档存储为相对路径

    在默认情况下,ArcGIS 10中地图文件mxd中添加的图层所引用的文件路径均为绝对路径.这就意味着,如果你在地图中引用了“D:\data\DEM.shp”文件,那map.mxd文件中保存的该层文件路 ...

  4. SQL Server添加MDW性能监控报表(转载)

    10.2 Data Collector与MDW Data Collection功能是SQL SERVER 2005版本提供的数据库监控报表的功能,通过定时地对数据库的语句运行情况,服务器各种资源的监控 ...

  5. Mybatis 插入null值报错

    解决方法: 1.在settings中配置 <setting name="jdbcTypeForNull" value="OTHER"/> MyBat ...

  6. redis-redigo及redis存储时间问题测试

    package main import ( "log" "github.com/garyburd/redigo/redis" "github.com/ ...

  7. CentOS6开启FTP及telnet服务教程

    先来开通CentOS6的FTP服务吧.telnet服务也一并学习学习吧.在安装好CentOS以后,需要设置Ftp和Telnet服务文件,才能启动Ftp和Telnet服务,可以通过远程控制进行开启. 开 ...

  8. EMR,电子病历(Electronic Medical Record)

    电子病历 电子病历(EMR,Electronic Medical Record),也叫计算机化的病案系统或称基于计算机的病人记录(CPR,Computer-Based Patient Record). ...

  9. 34、Shiro框架入门三,角色管理

    //首先这里是java代码,就是根据shiro-role.ini配置文件中的信息来得到role与用户信息的对应关系//从而来管理rolepublic class TestShiroRoleTest e ...

  10. PHP 位移运算符(<<左移和>>右移)

    位移运算符 << 位左移 左移运算的实质是将对应的数据的二进制值逐位左移若干位,并在空出的位置上填0,最高位溢出并舍弃.例 如 $a=10; $b=$a<<2; 则$b=40, ...