编程之美的第一章的第15节,讲的是构造数独,一开始拿到这个问题的确没有思路, 不过看了书中的介绍之后, 发现原来这个的求解思路和N皇后问题是一致的, 但是不知道为啥,反正一开始确实没有想到这个回溯法,知道是用回溯法求解之后,问题就变得容易了很多。
这里我们不打算实现数独的构造,相反的,我们实现一个数独求解器,以后妈妈再也不用担心我的数独了。

当然求解器的思路和构造数独的思路一样,都是回溯法搜索,这里不再过多说明。

程序运行说明:
1.把待求解的数独数据放到in.txt文件中, 程序会自动读取他,并将解输出到屏幕和out.txt文件中。

2.控制台颜色改变 使用了SetConsoleTextAttribute函数,具体使用细节可以参考 C/C++改变控制台输出字体的背景和颜色(windows)

3.关于程序运行时间统计,使用getTickCount()或者clock()都是可以的

运行效果如下:

红色表示部分就是求解出来的值了>_<~~

按照惯例下面是源码:

// ================【shudu.h】===========
#pragma once#define N 9
#include <fstream>typedef struct _SearchNode
{int row;int col;int data;
}SearchNode;class CShuDu
{
public:CShuDu();~CShuDu();void CreateSolution();  private:bool isOK(int row, int col);void solve(int searchNode_id);void writeFile(int id);void display(int id);void initShudu();void output(int id);private:int m_shudu[N + 2][N + 1];bool m_find;int m_solution_id;std::fstream outfile;SearchNode m_searchNode[N * N];int m_searchLength;
};
// =====================【shudu.cpp】==================
#include <windows.h>#include "ShuDu.h"
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>CShuDu::CShuDu()
{memset(m_shudu, 0, sizeof(m_shudu));m_find = false;m_solution_id = 0;outfile.open("out.txt", std::ios::out);m_searchLength = 0;memset(m_searchNode, 0, sizeof(m_searchNode));
}CShuDu::~CShuDu()
{outfile.close();
}void CShuDu::CreateSolution()
{initShudu();solve(1);
}bool CShuDu::isOK(int row, int col)
{bool isOK = true;// 列方向检测for (int i = 1; i != 10; i++){if (i == row)continue;isOK = (m_shudu[i][col] == m_shudu[row][col]) ? false : true;if (isOK == false)return isOK;}// 行检测for (int i = 1; i != 10; i++){if (i == col)continue;isOK = (m_shudu[row][i] == m_shudu[row][col]) ? false : true;if (isOK == false)return isOK;}// 区域检测int block_start_row = (row - 1) / 3 * 3 + 1;int block_start_col = (col - 1) / 3 * 3 + 1;for (int i = block_start_row; i != block_start_row + 3; i++){for (int j = block_start_col; j != block_start_col + 3; j++){if (i == row && j == col)continue;isOK = (m_shudu[i][j] == m_shudu[row][col]) ? false : true;if (isOK == false)return isOK;}}return isOK;
}void CShuDu::solve(int searchNode_id)
{if (m_find == true)return;if (m_searchLength + 1 == searchNode_id){//m_find = true;m_solution_id++;output(m_solution_id);return;}for (int i = 1; i != 10; i++){int row = m_searchNode[searchNode_id].row;int col = m_searchNode[searchNode_id].col;m_shudu[row][col] = i;if (isOK(row, col)){solve(searchNode_id + 1);row = m_searchNode[searchNode_id + 1].row;col = m_searchNode[searchNode_id + 1].col;m_shudu[row][col] = 0;}           }
}void CShuDu::display(int id)
{std::cout << "===========================第" << id << "组数独数据=============================" << std::endl;system("title 数独求解器 by zhyh2010 version 1.0");int search_id = 1;HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);for (int i = 1; i != 10; i++){for (int j = 1; j != 10; j++){if (i == m_searchNode[search_id].row&& j == m_searchNode[search_id].col){SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);search_id++;}               elseSetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);std::cout << m_shudu[i][j] << " ";}std::cout << std::endl;}std::cout << std::endl;std::cout << std::endl;SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY);
}void CShuDu::writeFile(int id)
{outfile << "===========================第" << id << "组数独数据=============================" << std::endl;for (int i = 1; i != 10; i++){for (int j = 1; j != 10; j++){outfile << m_shudu[i][j] << " ";}outfile << std::endl;}outfile << std::endl;outfile << std::endl;
}void CShuDu::output(int id)
{display(id);writeFile(id);//getchar();
}void CShuDu::initShudu()
{std::ifstream infile("in.txt");for (int i = 1; i != 10; i++){for (int j = 1; j != 10; j++){infile >> m_shudu[i][j];if (m_shudu[i][j] == 0){m_searchLength++;m_searchNode[m_searchLength].row = i;m_searchNode[m_searchLength].col = j;}}}
}
// =====================【数独求解器 main.cpp】==================
// @ author         :           zhyh2010
// @ date           :           20150624
// @ version        :           1.0
// @ description    :           C++
// =====================【数独求解器】==================#include <stdio.h>
#include "ShuDu.h"
#include <time.h>void main()
{//DWORD t_start = GetTickCount();auto t = clock();CShuDu instance;instance.CreateSolution();//DWORD t_end = GetTickCount();printf("程序运行时间为 %d ms\n", 1000*static_cast<float>(clock() - t)/CLOCKS_PER_SEC);
}

编程之美之数独求解器的C++实现方法相关推荐

  1. MATLAB 自动数独求解器(导入图片自动求解)

    做了一个导入图片自动求解数独的软件,不过由于目前是通过最小二乘法匹配数字的,所以导入图片中的数字最好不要是手写的..,图片大概就像这样: 使用效果: 完整代码: function sudokuApp ...

  2. C++数独求解器与生成器

    前几天笔者外出培训,刚刚学习了深度优先搜索,突然想到了数独的求解其实也可以用深搜实现,遂写了数独求解器与生成器. 1 数独求解器 1.1 预备 一开始,当然是头文件~ #include <ios ...

  3. 好用的z3数独求解器

    github 上发现一个好用 用z3 编写的数独求解器 传送门: https://github.com/dferri/z3-skyscrapers Generate a skyscrapers puz ...

  4. 数独输出Java_java – 使用回溯的数独求解器

    我最近一直致力于回溯数独求解算法,目前我想询问我应该如何将我的solve()方法从void更改为boolean. 我正在使用一个非常简单的回溯算法,它目前工作正常,但我宁愿有一个布尔值而不是一个空格, ...

  5. POJ 2676 Sudoku (数独求解器 DFS)

    题目链接 POJ2676 题目大意 输入n个数独,空格用0表示,填数独(符合的一种方案即可). 分析 类似于八皇后的简单搜索题,用DFS回溯法求解. 将空格的位置都记录下来,1个1个填下去,填不下去就 ...

  6. python数独解题器,Python中最短的数独求解器 – 它是如何工作的?

    那么,通过修正语法,可以使事情变得更容易: def r(a): i = a.find('0') ~i or exit(a) [m in[(ij)%9*(i/9^j/9)*(i/27^j/27|i%9/ ...

  7. 编程之美-构造数独(1)

    回溯法: #include <stdio.h> #include <stdlib.h> #include <time.h>int isShudu(int shudu ...

  8. 编程之美-构造数独方法整理

    [试题描述] 方法:

  9. c 语言写数独游戏,经典数独游戏+数独求解器—纯C语言实现

    [转]NGUI研究院之三种方式监听NGUI的事件方法(七) NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直 ...

最新文章

  1. Swift3.0语言教程替换子字符串
  2. Strategy Pattern(策略模式)
  3. PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
  4. 阿里云年会人机大战-技术大揭秘
  5. 怎么让饼状图里面显示百分比_教你用matplotlib绘制带有负值的饼状图
  6. asp.net ViewState详解
  7. Hbase 2.0 RegionObserver使用
  8. 在MaxCompute中利用bitmap进行数据处理
  9. polymer中的sort和filter
  10. 【Flink】Could not connect to BlobServer at address
  11. 浅谈FFT(快速博立叶变换)学习笔记
  12. Linux入门:usermod - 修改用户帐户信息
  13. python官网下载好慢-python 官网慢
  14. python和java先学哪个-java和python先学哪个
  15. ES6躬行记(3)——解构
  16. ip变更会影响账号登陆吗_代理IP的匿名度级别会影响自身稳定性吗
  17. [Asp.Net web api]基于自定义Filter的安全认证
  18. 百度知道1000指数的关键词留链接排名到第一的实战案例
  19. 大学计算机课思维导图,快速记忆和理解大学计算机思维导图
  20. ES8316耳机驱动可以差分输入支持录音PEQ调节

热门文章

  1. 林业调查规划资质全国林业单位办理认定标准和申请条件
  2. 20221219 圣诞节,音乐圣诞树
  3. 高级计算机软考科目,软考高级中哪个科目好考
  4. Unity编译器下载网址!!
  5. 国产处理器服务器操作系统安装(海之舟服务器操作系统安装说明)
  6. excel 单元格插入图片
  7. 找不到或无法加载主类 com.xxx.xxxxApplication
  8. 苏州大学计算机论文多少字,苏州大学本科论文格式
  9. 80老翁谈人生(151):老翁老眼昏花,读错了一个数量级
  10. bugku 把猪困在猪圈里