Sudoku Solver Backtracking
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的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- 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 ...
随机推荐
- 关于SoCFPGA 编译问答
1.在Qsys里面添加了一个新的组件(不是hps里面的东西),挂在Avalon总线上,如添加了一个新ADC组件,是否需要重新编译dtb. 答: 不需要. 2.修改了相关Qsys里面的东西,是否也需要 ...
- 仿淘宝详情转场(iOS,安卓没有这功能)
由于公司是做跨境电商的,所以对各大电商APP都有关注,最近看到淘宝iOS端(安卓没有)在商品详情点击加入购物车有一个动画效果特别赞,正好今天新版本上线,下午就抽了些时间研究了下. 主要思路是自定义转场 ...
- FMDBsqilte语句样式
举例: @"create table student(stuid integer primary key autoincrement,name text,number integer,the ...
- MVC Actionlink 参数说明
Html.ActionLink用于输出链接,以下是带参数的例子: @Html.ActionLink("编辑", "Edit", new {id= "1 ...
- Microsoft source-code annotation language (SAL) 相关
More info see: https://msdn.microsoft.com/en-us/library/hh916383.aspx Simply stated, SAL is an inexp ...
- Python 前端之JQuery
查找: 选择器 筛选器 操作: CSS 属性 文本 事件: 优化 扩展: Form表单验证 Ajax: 偷偷发请求 www.php100.com/manual/jquery http://blog.j ...
- Mybatis按SQL查询字段的顺序返回查询结果
在SpringMVC+Mybatis的开发过程中,可以通过指定resultType="hashmap"来获得查询结果,但其输出是没有顺序的.如果要按照SQL查询字段的顺序返回查询结 ...
- Icinga快速安装与配置
Icinga快速安装与配置/* body */body { margin: 20px; padding: 0; font-family: "Lucida Grande", &quo ...
- WCF第二天
消息 : 消息是一个独立的数据单元,它可能由几个部分组成,包括消息正文和消息头. 服务 : 服务是一个构造,它公开一个或多个终结点,其中每个终结点都公开一个或多个服务操作. 终结点 ...
- E. Santa Claus and Tangerines 二分答案 + 记忆化搜索
http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...