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 ...
随机推荐
- 3.Sqrt(x)
要求:Implement int sqrt(int x). Compute and return the square root of x. 解决方法: 1.牛顿法(Newton's method) ...
- hbase运行shell时ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing 的解决办法
这个问题困扰了我一天多的时间,百度搜索的前几条的答案也是很扯淡的,说什么把/etc/hosts文件下的127.0.1.1改成127.0.0.1就行了,我也只能呵呵了.今天早上起得很晚,中午迪哥请我们去 ...
- setTimeout
setTimeout(function () { $('#successTip').hide(); location = location; }, 3000);
- Ubuntu14.04通过pyenv配置多python
参考链接: https://github.com/yyuu/pyenv-virtualenv https://github.com/yyuu/pyenv http://seisman.info/pyt ...
- JavaScript高级程序设计笔记之面向对象
说起面向对象,大部分程序员首先会想到 类 .通过类可以创建许多具有共同属性以及方法的实例或者说对象.但是JavaScript并没有类的概念,而且在JavaScript中几乎一切皆对象,问题来了,Jav ...
- 处理返回结果(XML)
var xmlHttp function showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Br ...
- Ajax readystate 5种状态
Status 说明 0(Uninitialized) XMLHttpRequest 对象已经创建,但没调用 open 方法. 1(Loading) 调用 open 方法,但没调用 send 方法.(尚 ...
- 通过反射向将EF的实体映射配置加入到实体模型中
public AdminDbContext() : base("MemberDbContext") { //不使用代理创建导航属性,避免WCF序列化错误 Configuration ...
- Google上的Cookie Matching
Cookie Matching This guide explains how the Cookie Matching Service enables you to make more effecti ...
- Correspondence / ˏkɔris'pɔndәns / dictionary10-800.doc
I have taken courses in office administration, typing,reports and correspondence writing. Correspond ...