[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 ...
随机推荐
- Python_列表、字典、字符串、集合操作
一.list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.对于list的操作,我们要学会增删改查. 查 我们可以直接索引查找,也可以通过切片 ...
- 原生node实现本地静态页面的展示
var http = require("http"); var fs = require("fs"); var url = require("url& ...
- JZOJ 5809. 【NOIP2008模拟】数羊
5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms Memory Limits: ...
- Django配置邮箱登录
1.settings下配置 # AUTH 方法(支持邮箱登录) AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',) 2.views下逻辑如 ...
- mysql进阶三四五六
排序查询 一.语法 select 查询表 from 表 where 筛选条件 order by 排序列表[asc / desc] 特点: 1.asc:升序 desc:降序 2.排序列表之中支持单字段, ...
- 项目中遇到的ts问题汇总
报错关键词句 报错截图 解决 Declaration of public static field not allowed after declaration of public instance m ...
- nginx的进程模型
nginx采用的也是大部分http服务器的做法,就是master,worker模型,一个master进程管理站个或者多个worker进程,基本的事件处理都是放在woker中,master负责一些全局初 ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- 使用BootStrap网格布局进行一次演示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NOIP试题解析
NOIP试题解析 by QTY_YTQ noip2010关押罪犯(并查集) 题意是有n个罪犯关在两个监狱中,其中有m对罪犯有仇恨关系,如果有仇恨的罪犯关在一起会产生一定影响力的事件 ...