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 ...
随机推荐
- Delphi七个版本
第一次Zack Urlocker给我看一款尚未发布的名为Delphi的产品时,我意识到她将改变我的工作—还有许多其他软件开发者的工作. 我过去为使用Windows C++ 库而挣扎,而Delphi过去 ...
- AIDL学习
(转自)可以参见:http://www.2cto.com/kf/201406/312244.html 1.为什么要有AIDL? 无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在 ...
- 【T电商】 maven初识
PS:本篇博客,就是对于maven的一个简单的总结,认识.可能更多的是借鉴别人的看法,然后结合自己的使用,再加以说明. 首先,什么是maven: Apache Maven is a software ...
- PCI Express(一)- Connector
在FPGA4FUN上看到一篇介绍PCI-E的帖子,简单易懂,适合入门,特地搬过来 原文地址:http://www.fpga4fun.com/PCI-Express.html 前言: As PCI Ex ...
- php 连接mongodb 增查改删操作
查询 <?php $m=new MongoClient('mongodb://admin:admin@localhost:27017/admin'); $db=$m->hndb; $cc= ...
- codeforces 360 C - NP-Hard Problem
原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the ...
- c++学习笔记——字面值常量类
字面值常量类:数据成员都是字面值类型的聚合类是字面值常量类.如果一个类不是聚合类,但是它符合一下要求,则它也是个字面值常量类: 1.数据成员都必须是字面值类型. 2.类必须至少含有一个constexp ...
- spi 子系统
http://blog.csdn.net/ropenyuan/article/details/42269641 http://blog.chinaunix.net/uid-27406766-id-33 ...
- XML团队介绍发布!
三个臭皮匠,赛过诸葛亮 /********************************************************************/ 成员: 姓名:项浩哲 职务:项目经 ...
- Ubuntu中vi常用命令
在Ubuntu中经常需要修改某些文件,这里对vi中的一些常用操作作一下总结. 1.进入vi命令 vi filename: 打开或新建文件,并将光标置于第一行首 进入文件后,处于命令行模式(comman ...