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 ...
随机推荐
- 13. Reorder List
Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...
- (转)MyEclipse自动生成Hibernate实体类, oracle篇
转自http://blog.csdn.net/hejinwei_1987/article/details/9465529 1.打开 windows -> Open Perspective -&g ...
- C++学习基础六——复制构造函数和赋值操作符
1.什么是复制构造函数 复制构造函数:是构造函数,其只有一个参数,参数类型是所属类的类型,且参数是一个const引用. 作用:将本类的成员变量赋值为引用形参的成员变量. 2.什么是赋值操作符 赋值操作 ...
- Orcle常用语句
在SQLPlus界面的操作语句: 查看\设置每行内显示的字符数:show\set linesize [linesize] 查看\设置一次显示的行数:show\set pagesize [pagesiz ...
- redis 问题解决(MISCONF Redis is configured to save RDB snapshots)
(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d ...
- MVC 為頁面的list資料重新命名
function ReBookFileName() { $("#div_sortable").find("li[ ...
- mongodb账号安全操作
安装服务 mongod --install --serviceName mongodb --storageEngine=mmapv1 --dbpath i:\mongodb\data --journa ...
- Java中的容器
参考链接:Java容器与泛型(1) 认识容器,Java容器与泛型(2) ArrayList.LinkedList和Vector比较,Java容器与泛型(3) Hashset.TreeSet和Linke ...
- nginx的Location的总结以及rewrite规则的总结
Location的语法: location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上. 比如, ...
- Windows程序设计(第五版)学习:第二章 Unicode简介
第二章 Unicode简介 1,Windows通过双字节技术DBCS解决这个问题,代码页定义不同的字符集,称为ANSI字符集,比如日文为CP932,韩文为CP949,繁体中文为CP950,简体中文为C ...