[LeetCode] Sudoku Solver 解数独,递归,回溯
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.
![]()
A sudoku puzzle...
![]()
...and its solution numbers marked in red.
- 创建一个辅助函数,help(vector<vector<char> > & board,int i,int ii,int j,int jj),其中i为第几行,j 第几列,ii 小框中的第几行,jj 小框中第几列。
- help 函数4层for 循环遍历整个9*9 矩阵,说是4层,跟两次for 是一样的,i 和j 的步长是3,for(;i<9;i+=3),因为这4个变量的初始化在函数传入的时候,所以在其循环结束时候尾部赋0,其中一个不好的地方。
- 4层循环内部,首先判断当前格board[i+ii][j+jj]是否为数字,不然继续循环。
- 创建标记finish = false,标记接下来的填入(当前格为空)能否完成整个矩阵,用于控制跳出填写,和如果填写失败修复当前格为'.' 回溯。
- 遍历1-9 尝试填写,需要用到bool canIn=true,判断行,列,框中的情况,如果通过判断则修改当框为遍历的数字,递归下一格,输入当前格的i ii j jj,便可以,这是第二个不好的地方,判断没有写为外部函数,另外函数的参数设置导致了很多细节问题。
- 下层返回的值赋予 finish ,如果为false ,表示当前填写不行,继续遍历,如果为true,跳出填写的遍历。
- 结束遍历后判断是否已经填写整个矩阵,如果finish 为false,表示失败了,则当前格,然后返回false,成功先别返回。
- 外部4层循环结束返回true。
所以在外层循环后返回true,是假如例子上的情况,最后一个格不为空,那么按上面逻辑是不会进入内部的,而如果能够全部遍历完整个矩阵,那么表示没有在内部返回,等价于成功填写了。
之所以使用 ii jj,是为了可以方便处理3*3框,在能否填写的时候,需要注意跳过自身与自身判断,先判断行列,然后框的时候变可以跳过当前框所在的行列了。
#include <vector>
#include <iostream>
#include <iterator>
using namespace std; class Solution {
public:
void solveSudoku(vector<vector<char> > &board) {
help(board,,,,);
}
bool help(vector<vector<char> > & board,int i,int ii,int j,int jj){
for(;i<;i+=){
for(;ii<;ii++){
for(;j<;j+=){
for(;jj<;jj++){
if(board[i+ii][j+jj]!='.') continue;
bool finish = false;
for(char c='';c<=''&&!finish;c++){
bool canIn= true;
for(int k=;k<&&canIn;k++){
if(k!=i+ii&&board[k][j+jj]==c) canIn = false;
if(k!=j+jj&&board[i+ii][k]==c) canIn = false;
}
for(int ti=;ti<&&canIn;ti++){
if(ti == ii) continue;
for(int tj=;tj<&&canIn;tj++){
if(tj==jj) continue;
if(board[ti+i][tj+j]==c) canIn = false;
}
}
if(canIn==false) continue;
board[i+ii][j+jj] = c;
finish = help(board,i,ii,j,jj);
}
if(!finish){ board[i+ii][j+jj] = '.'; return false; }
}
jj = ;
}
j=;
}
ii=;
}
return true;
}
}; int main()
{
vector<char> line;
vector<vector<char> > board;
line = {'','','.','.','','.','.','.','.'};
board.push_back(line);
line.clear();
line = {'','.','.','','','','.','.','.'};
board.push_back(line);
line.clear();
line = {'.','','','.','.','.','.','','.'};
board.push_back(line);
line.clear();
line = {'','.','.','.','','.','.','.',''};
board.push_back(line);
line.clear();
line = {'','.','.','','.','','.','.',''};
board.push_back(line);
line.clear();
line = {'','.','.','.','','.','.','.',''};
board.push_back(line);
line.clear();
line = {'.','','.','.','.','.','','','.'};
board.push_back(line);
line.clear();
line = {'.','.','.','','','','.','.',''};
board.push_back(line);
line.clear();
line = {'.','.','.','.','','.','.','',''};
board.push_back(line);
Solution sol;
sol.solveSudoku(board);
for(int i=;i<board.size();i++){
copy(board[i].begin(),board[i].end(),ostream_iterator<char>(cout," "));
cout<<endl;
}
return ;
}
[LeetCode] Sudoku Solver 解数独,递归,回溯的更多相关文章
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- Sudoku Solver, 求数独
问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode OJ:Sudoku Solver(数独游戏)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver(迭代)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- PHP使用CURL_MULTI实现多线程采集
$connomains = array( "http://www.baidu.com/", "http://www.hao123.com/", "ht ...
- python面向对象之反射和内置方法
一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...
- Python入门第一课——Python的起源、发展与前景!
我们在做任何一件事情之前,我们都会通过各种渠道去搜集事情的信息,了解事情的来龙去脉,学习一门编程语言也是如此,只有知根知底,我们才能有明确的方向和目标,以及底气去完成这件事情,今天我带大家来看看Pyt ...
- RF、GBDT、XGBOOST常见面试算法整理
1. RF(随机森林)与GBDT之间的区别 相同点: 1)都是由多棵树组成的 2)最终的结果都是由多棵树一起决定 不同点: 1) 组成随机森林的树可以是分类树也可以是回归树,而GBDT只由回归树组 ...
- oracle 基本函数
1)字符串函数---length()函数 用于返回字符串长度 select t.name,length(t.name) from tb_person t 2)向左补全字符串---LPAD()函数 L ...
- java中equals和==
https://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html
- Wordpress 数据库查询错误 Call to a member function get_results() on null
在插件中的一个文件使用如下代码,无法查询 <body> <?php global $wpdb; $sql = ""; $sql = "SELECT * ...
- Codeforces 1139D(期望dp)
题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...
- RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
认证组件.权限组件.频率组件总结: 认证组件格式: 1 写一个认证类 from rest_framework.authentication import BaseAuthentication cla ...
- nyoj 题目6 喷水装置
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...