【原创】一个基于简单剪枝的DFS解数独程序
问题来源:leetCode Sudoku Solver
Write a program to solve aSudoku puzzle by filling the empty cells.
Empty cells are indicated by the character *.*.
You may assume that there will be only one unique solution.
问题链接:
https://oj.leetcode.com/problems/sudoku-solver/
功能:
自动解数独题
大概方案:
采用以行为单位的DFS来寻找答案。
提纲:
0、对全部数据进行候选数预处理,记录下每个空格的候选数。
1、根据候选数表和当前结果表,以行为单位,递归产生所有可能的行,并加入结果数组,然后对每一个产生的可能行进行新一轮的递归调用。这里有一个互相递归调用的行为。
2、若无法产生可能的行或产生完所有行的下一级返回假,则返回假。
3、若有一个下级递归返回真或者最后一行中成功产生了可能的行,返回真。
主要方法概览:
(注意:候选数表为全局数组,所有方法可以直接访问)
0、bool tryRow(introw,vector<vector<int> > &res);
该方法为对某行进行数字试填,row为某行的行号,res为当前结果数组。
1、bool fillRow(introw,int col,vector<int> &now,vector<vector<int> >&res);
该方法为对特定的格子进行数字试填,row为行号,col为列号,now为当前行中已填的数字,res为结果数组。
2、以上两种方法互相递归调用。对于特定新行,调用tryRow方法,而填新行内的数则递归调用fillRow,当当前行已经填满后,在fillRow中调用新一轮的tryRow方法进入下一行。
Solution类的全部代码和注释:(\t被吞,用/*...*/来缩进)
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
class Solution
{
public:
/*......*/ int sel[9][9][10];
// 候选数数组,sel[x][y][0]标记横坐标为x,纵坐标为y的格子是否有数字,有则为该数字,
// 待填则sel[x][y][num](1<= num <=9)为1表示该格子可选数包括num.
/*......*/ Solution()
/*......*/ {
/*.........*/ for (int i = 0;i < 9;i++)
/*............*/ for (int j = 0;j <9;j++)
/*...............*/ for (int p = 0;p <10;p++)
/*..................*/ sel[i][j][p] = 1;
/*......*/ }
/*......*/ voidsolveSudoku(vector<vector<char> > &board) // 主处理方法
/*......*/ {
/*.........*/ vector<vector<int>> res;
/*.........*/ init(board); // 进行候选数初始化处理
/*.........*/ tryRow(0,res);
/*.........*/ int len1 = board.size();
/*.........*/ int len2 = board[0].size();
/*.........*/ for (int i = 0;i <len1;i++)
/*............*/ for (int j = 0;j <len2;j++)
/*...............*/ if (board[i][j] == *.*)
/*..................*/ board[i][j] =res[i][j] + *0*;
/*......*/ }
/*......*/ bool tryRow(introw,vector<vector<int> > &res)
/*......*/ {
/*.........*/ if (row >= 9)
/*.........*/ {
/*............*/ return true;
/*.........*/ }
/*.........*/ bool flag = false;
/*.........*/ vector<int> now(10);
/*.........*/ flag =fillRow(row,0,now,res);
/*.........*/ return flag;
/*......*/ }
/*......*/ bool fillRow(int row,intcol,vector<int> &now,vector<vector<int> > &res)
/*......*/ {
/*.........*/ bool flag = false;
/*.........*/ if (col >= 9)
/*.........*/ {
/*............*/ res.push_back(now);
/*............*/ flag = tryRow(row +1,res);
/*............*/ if (flag == false)
/*...............*/ res.pop_back();
/*............*/ return flag;
/*.........*/ }
/*.........*/ if (sel[row][col][0] != 0)
/*.........*/ {
/*...............*/ now[col] =sel[row][col][0];
/*...............*/ flag = fillRow(row,col+ 1,now,res);
/*...............*/ return flag;
/*.........*/ }
/*.........*/ int hash[10] = {0}; // now中现存的数为1,该数组中为1的数不能选
/*.........*/ for (int i = col - 1;i >=0;i--) // 判断当前行中需要更新的候选数
/*............*/ hash[now[i]] = 1;
/*.........*/ update(row,col,hash,res); // 该函数继续更新该格子的候选数
/*......*/ for (int i = 1;i <= 9;i++)
/*.........*/ {
/*............*/ if (hash[i] == 0&& sel[row][col][i] == 1)
/*............*/ {
/*...............*/ now[col] = i;
/*...............*/ flag = fillRow(row,col+ 1,now,res);
/*...............*/ if (flag)
/*..................*/ return true;
/*............*/ }
/*.........*/ }/*...*/
/*.........*/ return flag;
/*......*/ }/*...*/
/*......*/
/*......*/ void update(int row,int col,int*hash,vector<vector<int> > &res)
/*......*/ {
/*.........*/ int top = getTop(row);
/*.........*/ int left = getLeft(col);
/*.........*/ for (int i = row - 1;i >=0;i--) // 既有结果列中的数不能选
/*.........*/ {
/*...............*/ hash[res[i][col]] = 1;
/*.........*/ }
/*.........*/ if (row > top)
/*.........*/ {
/*............*/ int count = row - top;
/*............*/ for (int i = 0;i <count;i++)
/*...............*/ for (int j = 0;j <3;j++)
/*..................*/hash[res[top+i][left+j]] = 1;
/*.........*/ }
/*......*/ }
/*......*/ voidinit(vector<vector<char> > &board)
/*......*/ {
/*.........*/ int len1 = board.size();
/*.........*/ int len2 = board[0].size();
/*.........*/ for (int i = 0;i <len1;i++)
/*............*/ for (int j = 0;j <len2;j++)
/*............*/ {
/*...............*/ if (board[i][j] != *.*)
/*...............*/ {
/*..................*/ setRow(i,board[i][j]- *0*);
/*..................*/ setCol(j,board[i][j]- *0*);
/*..................*/setCel(i,j,board[i][j] - *0*);
/*..................*/ sel[i][j][0] =board[i][j] - *0*;/*...*/
/*...............*/ }
/*...............*/ else
/*..................*/ sel[i][j][0] = 0;
/*............*/ }
/*......*/ }
/*......*/ void setRow(int i,int num)
/*......*/ {
/*.........*/ for (int j = 0;j < 9;j++)
/*.........*/ {
/*............*/ sel[i][j][num] = 0;
/*.........*/ }
/*......*/ }
/*......*/ void setCol(int j,int num)
/*......*/ {
/*.........*/ for (int i = 0;i < 9;i++)
/*.........*/ {
/*............*/ sel[i][j][num] = 0;
/*.........*/ }
/*......*/ }
/*......*/ void setCel(int x,int y,intnum)// 求方格的左上坐标
/*......*/ {
/*.........*/ int top,left;
/*.........*/ top = getTop(x);
/*.........*/ left = getLeft(y);
/*.........*/ for (int i = 0;i < 3;i++)
/*............*/ for (int j = 0;j <3;j++)
/*...............*/ sel[i+top][j+left][num]= 0;
/*......*/
// 该函数同getTop一样,用来求位于x行y列的格子所在的正方形格的位置。
/*......*/ int getLeft(int col)
/*......*/ {
/*.........*/ int tmp;
/*.........*/ int left;
/*.........*/ tmp = col % 9;
/*.........*/ if (tmp >= 6)
/*.........*/ {
/*............*/ left = 6;
/*.........*/ }
/*.........*/ else if (tmp >= 3)
/*............*/ left = 3;
/*.........*/ else
/*............*/ left = 0;
/*.........*/ return left;
/*......*/ }
/*......*/ int getTop(int row)
/*......*/ {
/*.........*/ int tmp;
/*.........*/ int top;
/*.........*/ tmp = row % 9;
/*.........*/ if (tmp >= 6)
/*.........*/ {
/*............*/ top = 6;
/*.........*/ }
/*.........*/ else if (tmp >= 3)
/*.........*/ {
/*............*/ top = 3;
/*.........*/ }
/*.........*/ else
/*............*/ top = 0;
/*.........*/ return top;
/*......*/ }
};
// 测试函数
int main(void)
{
/*...*/ vector<char> now;
/*...*/ vector<vector<char> >res;
/*...*/ int c;
/*...*/ Solution *p = new Solution();
/*...*/ while ((c = getchar()) != EOF)
/*...*/ {
/*......*/ if (c != '\n')
/*.........*/ now.push_back(c);
/*......*/ else
/*......*/ {
/*.........*/ res.push_back(now);
/*.........*/ now.clear();
/*......*/ }
}
/*...*/ p->solveSudoku(res);
/*...*/ return 0;
}
【原创】一个基于简单剪枝的DFS解数独程序的更多相关文章
- 用qpython3写一个最简单的发送短信的程序
到目前为止并没有多少手机应用是用python开发的,不过qpython可以作为一个不错的玩具推荐给大家来玩. 写一个最简单的发送短信的程序,代码如下: #-*-coding:utf8;-*- #qpy ...
- HDU 1426 Sudoku Killer(dfs 解数独)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...
- [原创]python之简单计算器(超详解,只有基本功能+-*/,还有括号处理)
不想看过程的话,直接看文章最后的正式源码 作业需求及分析: 流程图 https://www.processon.com/diagraming/580c5276e4b03c844a5a9716 初期感 ...
- 一个基于Socket的http请求监听程序实现
首先来看以下我们的需求: 用java编写一个监听程序,监听指定的端口,通过浏览器如http://localhost:7777来访问时,可以把请求到的内容记录下来,记录可以存文件,sqlit,mysql ...
- 用scala实现一个基于TCP Socket的快速文件传输程序
这是用scala实现的一个简单的文件传输程序. 服务端 package jpush import java.io.{DataInputStream, File, FileOutputStream} i ...
- 如何使用 Docker 部署一个基于 Play Framework 的 Scala Web 应用?
本文作者 Jacek Laskowski 拥有近20年的应用程序开发经验,现 CodiLime 的软件开发团队 Leader,曾从 IBM 取得多种资格认证.在这篇博文中,Jacek 分享了 Wars ...
- 新项目架构从零开始(三)------基于简单ESB的服务架构
这几个月一直在修改架构,所以迟迟没有更新博客. 新的架构是一个基于简单esb的服务架构,主要构成是esb服务注册,wcf服务,MVC项目构成. 首先,我门来看一看解决方案, 1.Common 在Com ...
- 使用python解数独
偶然发现linux系统附带的一个数独游戏,打开玩了几把.无奈是个数独菜鸟,以前没玩过,根本就走不出几步就一团浆糊了. 于是就打算借助计算机的强大运算力来暴力解数独,还是很有乐趣的. 下面就记录一下我写 ...
- Kcptun 是一个非常简单和快速的,基于KCP 协议的UDP 隧道,它可以将TCP 流转换为KCP+UDP 流
本博客曾经发布了通过 Finalspeed 加速 Shadowsocks 的教程,大家普遍反映能达到一个非常不错的速度.Finalspeed 虽好,就是内存占用稍高,不适合服务器内存本来就小的用户:而 ...
随机推荐
- Delphi 的绘图功能(29篇博客)
http://www.cnblogs.com/del/category/123038.html
- [Cocos2d-x]Mac下运行HelloCpp For Android
2013年12月22日 一.简介: Mac下运行Cocos2d-x的samples和新建的HelloCocos2dx项目 二.内容: 环境: OS:mac OS X 10.9.1 IDE:Androi ...
- Oracle varchar2最大支持长度(转)
oerr ora 0650206502, 00000, "PL/SQL: numeric or value error%s"// *Cause: An arithmetic, nu ...
- C++获取文件大小常用技巧
C++编程语言在程序开发应用中能够帮助我们轻松的完成许多功能需求.比如今天为大家介绍的C++获取文件大小的方法,就可以以多种方式轻松的实现.现在将会实现方法呈现给大家,以便大家参考. C++获取文件大 ...
- 【ThinkingInC++】52、函数内部的静态变量
/** * 书本:[ThinkingInC++] * 功能:函数内部的静态变量 * 时间:2014年9月17日18:06:33 * 作者:cutter_point */ #include " ...
- hdu4389(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意:f(x)表示x各位的数字和. 给定1<=L<=R<=10^9, 求[L, ...
- hdu1028(整数划分问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题 整数划分 --- 一个老生长谈的问题: 描述 整数划分是一个经典的问题.请写一个程 ...
- AOP 之 6.1 AOP基础 ——跟我学spring3(转)
http://jinnianshilongnian.iteye.com/blog/1418596
- hdu2089(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间[a,b]内不含有62或4的数的个数. 分析:数位dp,dp[pos][0]表示到第 ...
- mysql事务、触发器、视图、存储过程、函数
存储过程: procedure 概念类似于函数,就是把一段代码封装起来, 当要执行这一段代码的时候,可以通过调用该存储过程来实现. 在封装的语句体里面,可以用if/else, case,while等控 ...