武士数独求解问题
老师布置了一个作业,用代码求解武士数独问题,我直接用dfs+一些优化做出来了(偷了一个大懒,具体大家看代码吧),图片就是武士数独,时间关系就不排版了。核心代码就是dfs函数,我用answer封装了一下,优化思路的话acwing,y总有教过。因为老师给的题目比较简单,每个99的数独都有唯一解,我就偷懒了,想要真正地实现代码,我提供一个思路:把中间的99数独的解全部求解出来,然后在依次求解剩余的四个。为啥这样,就自己想吧(累了,老师还不一定改)。

直接上代码

#include<bits/stdc++.h>using namespace std;const int N=9;int nums;
int ones[1<<N];//记录每个整数二进制的表达式中有多少个1
int f1[1<<N];//二进制与整数的映射
int row[N] , col[N] , cell[3][3];//约束条件 //inline关键字,可以加速非递归函数的编译时速度
inline int lowbit(int x){//(返回x最低位为1对应的二进制数)return x & -x;
}//初始化
void init(){for(int i=0 ; i < N; i++) row[i] = col[i] = (1 << N) - 1;//更新行列的可选数位1-9用二进制表示//同理9*9 for(int i = 0 ; i < 3 ; i++)//(3x3)for(int j = 0 ; j < 3 ; j++ )cell[i][j] = ( 1 << N) - 1;}inline int get(int x,int y){return row[x] & col[y] & cell[x/3][y/3];//求解当前行和列中的的可选数的二进制表示 }int dfs(int cnt,char str [][9],char res[][9]){//如果没有空格则结束递归 if(!cnt){//(输出方案)for(int i = 0 ; i < 9 ; i++){for(int j = 0 ; j < 9 ; j++){res[i][j] = str[i][j];   }}nums++;   }else{//找出可选方案数最少的空格int minv = 10;int x , y;//(记录最少空格的坐标)for(int i = 0 ; i < N ; i++)for(int j = 0 ; j < N ; j++)if(str[i][j] == '.'){int t = ones[get(i,j)];if(t < minv){minv = t;x = i;y = j;}}for(int i=get(x,y) ; i ; i -= lowbit(i)){int t = f1[lowbit(i)];//修改状态row[x] -= 1 << t;col[y] -= 1 << t;cell[x/3][y/3] -= 1 << t;str[x][y] = '1' + t;dfs(cnt - 1 , str , res);//恢复现场str[x][y] = '.';cell[x/3][y/3] += 1 << t;col[y] += 1 << t;row[x] += 1 << t;}}return nums;}int answer(char str [][9] , char res[][9]){nums = 0;//记录9*9数独解的个数 for(int i = 0;i< N ; i++) f1[1<<i] = i;for(int i = 0;i< 1 << N; i++){int s = 0;for(int j = i; j ; j -= lowbit(j)) s++;//i的二进制中有s个1ones[i] = s;}init();//初始化int cnt = 0;for(int i = 0 ; i < N; i++)for(int j = 0 ; j < N ; j++)if(str[i][j] != '.'){//(修改每行每列,每个3x3在此坐标处可选数字集合)int t = str[i][j] - '1';row[i] -= 1 << t;col[j]-= 1 << t;cell[i/3][j/3] -= 1 << t;}else cnt++;//(记录有多少个空格)int nums = dfs(cnt,str,res);//(递归深搜)return nums;
}int main(){/*{'.', '.', '.', '5', '.', '.', '9', '2', '7'},{'2', '3', '.', '1', '7', '9', '.', '6', '.'},{'.', '.', '.', '.', '4', '.', '.', '3', '5'},{'1', '.', '3', '4', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '8', '.', '.', '.'},{'8', '.', '.', '.', '.', '2', '.', '.', '.'},{'.', '1', '4', '7', '2', '.', '.', '.', '6'},{'6', '.', '.', '9', '3', '1', '7', '.', '.'},{'.', '7', '2', '8', '.', '4', '.', '.', '.'}{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '8', '.', '.', '7', '.', '.', '3', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'7', '.', '.', '.', '1', '.', '.', '.', '3'},{'.', '.', '.', '.', '.', '6', '.', '.', '.'},{'.', '.', '2', '.', '.', '.', '4', '.', '.'},{'.', '.', '.', '.', '5', '.', '.', '.', '.'},{'.', '4', '.', '.', '6', '.', '.', '9', '.'},{'6', '.', '.', '.', '.', '.', '3', '.', '.'}*///第一组 char str6[][9]= {{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '2', '5', '8', '.', '.', '.'},{'.', '.', '.', '.', '9', '4', '.', '.', '.'},{'.', '.', '.', '.', '.', '1', '.', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '.'}};         char str3[][9]= {{'.', '.', '.', '.', '1', '6', '.', '.', '.'},{'.', '2', '.', '.', '.', '.', '.', '1', '8'},{'.', '8', '.', '2', '.', '3', '4', '.', '.'},{'2', '.', '.', '.', '.', '.', '.', '.', '9'},{'.', '.', '4', '.', '.', '.', '6', '.', '3'},{'.', '.', '5', '.', '.', '.', '.', '2', '1'},{'.', '4', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '9', '.', '.', '8', '3', '5', '7'},{'8', '.', '.', '.', '5', '.', '.', '.', '.'}};         char str1[][9]= {{'.', '2', '3', '.', '.', '7', '5', '.', '.'},{'.', '.', '7', '.', '.', '.', '1', '.', '.'},{'.', '.', '4', '.', '.', '3', '8', '.', '.'},{'3', '.', '.', '.', '6', '8', '.', '9', '.'},{'1', '.', '.', '.', '7', '2', '6', '.', '5'},{'.', '.', '.', '5', '.', '.', '.', '.', '.'},{'.', '.', '.', '8', '.', '.', '.', '.', '.'},{'.', '9', '.', '.', '.', '.', '.', '2', '.'},{'.', '4', '6', '.', '.', '.', '.', '8', '.'}}; char str2[][9]= {{'3', '.', '4', '.', '.', '7', '.', '2', '.'},{'.', '.', '.', '.', '2', '.', '.', '8', '.'},{'.', '8', '.', '5', '.', '1', '.', '.', '.'},{'5', '.', '.', '.', '.', '.', '3', '.', '.'},{'6', '2', '.', '4', '.', '.', '.', '.', '1'},{'.', '.', '7', '.', '.', '.', '.', '.', '.'},{'.', '.', '.', '.', '.', '6', '.', '.', '5'},{'.', '1', '8', '9', '.', '.', '.', '.', '.'},{'4', '.', '.', '8', '1', '3', '7', '.', '.'}}; char str4[][9]= {{'.', '8', '.', '.', '.', '.', '.', '4', '.'},{'.', '3', '5', '.', '.', '7', '.', '.', '9'},{'.', '.', '4', '.', '2', '5', '8', '.', '.'},{'.', '.', '.', '.', '.', '.', '.', '.', '8'},{'.', '6', '9', '.', '.', '.', '4', '2', '.'},{'.', '.', '.', '.', '.', '.', '1', '.', '.'},{'6', '7', '.', '8', '.', '.', '.', '.', '1'},{'.', '4', '.', '2', '.', '.', '.', '.', '5'},{'.', '.', '1', '.', '7', '.', '3', '.', '.'}}; char str5[][9]= {{'.', '.', '.', '.', '.', '.', '.', '7', '.'},{'3', '5', '7', '8', '.', '2', '.', '.', '.'},{'.', '.', '.', '.', '.', '7', '.', '1', '3'},{'.', '.', '.', '.', '2', '.', '.', '9', '.'},{'5', '.', '.', '9', '.', '.', '.', '.', '.'},{'6', '.', '.', '.', '8', '3', '.', '.', '.'},{'8', '.', '2', '4', '.', '.', '7', '.', '.'},{'.', '.', '.', '.', '9', '.', '2', '.', '.'},{'.', '4', '3', '.', '.', '5', '.', '.', '.'}}; char res[5][9][9];char final[21][21]={'.'} ;int res1 = answer(str1,res[0]);int res2 = answer(str2,res[1]);int res3 = answer(str3,res[2]);int res4 = answer(str4,res[3]);int res5 = answer(str5,res[4]);for(int i = 0 ; i < 9 ; i++){for(int j = 0 ; j < 9 ; j++){final[i][j] = res[0][i][j];}}for(int i = 0 ; i < 9 ; i++){for(int j = 12 ; j < 21 ; j++){final[i][j] = res[1][i][j-12];}}for(int i =  6; i <6+9 ; i++){for(int j = 6; j <6+9 ; j++){final[i][j] = res[2][i-6][j-6];}}for(int i = 12 ; i < 12 + 9 ; i++){for(int j = 0 ; j < 9 ; j++){final[i][j] = res[3][i-12][j];}}for(int i = 12 ; i < 12+9 ; i++){for(int j = 12 ; j < 12+9 ; j++){final[i][j] = res[4][i-12][j-12];}}for(int i = 0; i < 21 ; i++){for(int j = 0 ; j < 21 ; j++){cout<<final[i][j]<<" ";}cout<<endl;}cout<<endl;//第二组 char str33[][9]= {{'.', '.', '.', '.', '.', '.', '5', '.', '.'},{'.', '6', '.', '.', '.', '.', '.', '7', '.'},{'7', '.', '2', '8', '6', '.', '.', '.', '4'},{'.', '.', '4', '.', '1', '3', '.', '.', '2'},{'6', '.', '.', '.', '.', '.', '.', '1', '.'},{'.', '.', '.', '9', '2', '.', '.', '.', '5'},{'.', '4', '.', '.', '.', '8', '.', '.', '7'},{'.', '5', '.', '3', '.', '4', '1', '.', '.'},{'9', '.', '.', '.', '.', '.', '.', '.', '.'}};         char str11[][9]= {{'.', '.', '1', '.', '2', '.', '3', '.', '7'},{'.', '4', '.', '.', '.', '7', '6', '2', '.'},{'.', '.', '.', '.', '.', '.', '9', '.', '.'},{'.', '.', '4', '5', '1', '9', '.', '7', '.'},{'.', '.', '.', '4', '.', '3', '2', '9', '.'},{'.', '6', '.', '.', '.', '.', '.', '.', '.'},{'.', '2', '.', '.', '.', '5', '.', '.', '.'},{'.', '7', '.', '.', '9', '.', '.', '6', '.'},{'.', '.', '6', '8', '.', '.', '7', '.', '2'}};    char str22[][9]= {{'.', '9', '.', '.', '.', '.', '.', '.', '.'},{'.', '.', '2', '7', '.', '3', '.', '.', '.'},{'7', '.', '.', '1', '5', '.', '.', '.', '6'},{'.', '1', '.', '.', '.', '9', '8', '.', '.'},{'2', '.', '.', '5', '.', '.', '.', '6', '.'},{'.', '.', '.', '6', '1', '.', '.', '.', '.'},{'5', '.', '.', '4', '3', '.', '.', '.', '9'},{'.', '7', '.', '9', '2', '6', '3', '4', '.'},{'.', '.', '4', '.', '.', '.', '.', '.', '1'}};char str44[][9]= {{'5', '9', '2', '.', '7', '.', '.', '4', '.'},{'.', '.', '.', '1', '.', '.', '.', '5', '.'},{'3', '.', '6', '5', '.', '.', '9', '.', '.'},{'8', '.', '.', '.', '2', '.', '.', '.', '.'},{'7', '.', '.', '.', '.', '.', '8', '.', '3'},{'.', '2', '.', '.', '.', '6', '.', '.', '.'},{'.', '.', '.', '.', '6', '.', '.', '.', '9'},{'.', '4', '.', '.', '.', '.', '1', '.', '.'},{'.', '5', '.', '9', '.', '.', '7', '.', '4'}};    char str55[][9]= {{'.', '.', '7', '.', '.', '.', '.', '.', '9'},{'1', '.', '.', '.', '.', '4', '.', '.', '3'},{'.', '.', '.', '9', '.', '2', '.', '.', '.'},{'8', '.', '.', '3', '.', '.', '.', '.', '.'},{'.', '.', '.', '4', '.', '.', '.', '7', '.'},{'.', '.', '.', '.', '2', '.', '1', '.', '4'},{'.', '.', '2', '.', '.', '7', '.', '5', '8'},{'.', '.', '6', '.', '.', '.', '.', '4', '.'},{'9', '.', '5', '.', '.', '.', '2', '.', '1'}};    char ress[5][9][9];char finall[21][21]={'.'} ;int res11 = answer(str11,ress[0]);int res22 = answer(str22,ress[1]);int res33 = answer(str33,ress[2]);int res44 = answer(str44,ress[3]);int res55 = answer(str55,ress[4]);
//      for(int i = 0 ; i < 5 ; i++){//          for(int j = 0 ; j < 9 ; j++){//              for(int k = 0 ; k < 9 ; k ++){//                  cout<<ress[i][j][k]<<" ";
//              }
//          cout<<endl;
//          }
//          cout<<endl;
//      }for(int i = 0 ; i < 9 ; i++){for(int j = 0 ; j < 9 ; j++){finall[i][j] = ress[0][i][j];}}for(int i = 0 ; i < 9 ; i++){for(int j = 12 ; j < 21 ; j++){finall[i][j] = ress[1][i][j-12];}}for(int i =  6; i <6+9 ; i++){for(int j = 6; j <6+9 ; j++){finall[i][j] = ress[2][i-6][j-6];}}for(int i = 12 ; i < 12 + 9 ; i++){for(int j = 0 ; j < 9 ; j++){finall[i][j] = ress[3][i-12][j];}}for(int i = 12 ; i < 12+9 ; i++){for(int j = 12 ; j < 12+9 ; j++){finall[i][j] = ress[4][i-12][j-12];}}for(int i = 0; i < 21 ; i++){for(int j = 0 ; j < 21 ; j++){cout<<finall[i][j]<<" ";}cout<<endl;}return 0;
}

武士数独求解思路+代码实现(部分)相关推荐

  1. 数独游戏的两种编程思路+代码

    ###数独 方法一: 设定三个方法:分别为行不重复,列不重复,单元格不重复:在判断是否重复的时候用了一个Boolean数组,默认值为false,若角标位置为true时那么说明已经重复了 需求:判断是否 ...

  2. Python解武士数独问题

    组合数学题如下: ![](https://img-blog.csdnimg.cn/6c922566babc4a87baace8e84078376d.png 参考这位博主的的链接:Python解9*9数 ...

  3. linux下多线程验证数独,6.6.1 数独求解服务器

    6.6.1 数独求解服务器 假设有这么一个网络编程任务:写一个求解数独的程序(Sudoku Solver),并把它做成一个网络服务. Sudoku Solver 是我喜爱的网络编程例子,它曾经出现在& ...

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

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

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

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

  6. 编程之美之数独求解器的C++实现方法

    编程之美的第一章的第15节,讲的是构造数独,一开始拿到这个问题的确没有思路, 不过看了书中的介绍之后, 发现原来这个的求解思路和N皇后问题是一致的, 但是不知道为啥,反正一开始确实没有想到这个回溯法, ...

  7. php 数独求解,php求解数独

    <?php /*     php数独求解,时间大约在1分钟 */ $nums[0] = array(0, 5, 0, 0, 1, 0, 0, 0, 9, ); $nums[1] = array( ...

  8. Leetcode各种题型题目+思路+代码(共176道题)

    文章目录 第一章:Leetcode 每日很多题 1.Leetcode-1047 删除字符串中的所有相邻重复项 2.剑指 Offer 53 - I. 在排序数组中查找数字 I 3.Leetcode704 ...

  9. OpenCV玩九宫格数独(三):九宫格生成与数独求解

    前言 在此之前,OpenCV玩九宫格数独(一)和(二)分别介绍了如何从九宫格图片中提取出已知数字和如何用knn训练数字识别模型.在这些前期工作都已经完成的基础上,接下来我们需要做什么呢? 我们要做的有 ...

最新文章

  1. 愚蠢的CNN,换个马甲就认不出猫!但,这病能治 | ICLR Oral
  2. 【OpenGL】十、OpenGL 绘制点 ( 初始化 OpenGL 矩阵 | 设置投影矩阵 | 设置模型视图矩阵 | 绘制点 | 清除缓冲区 | 设置当前颜色值 | 设置点大小 | 绘制点 )
  3. 新成立的Scala中心将重点关注教育和Scala社区
  4. 原来数学才是世界上最浪漫的学科!
  5. sql 大数据量插入优化
  6. html项目_Python Selenium项目实战之添加发送HTML测试报告邮件!
  7. Net::SSH::Perl 包 与 Net::OpenSSH 包的 性能对比代码
  8. 三维重建系列之COLMAP: Structure-from-Motion Revisited
  9. python输出文件夹路径_python如何获取文件夹下第一层文件的路径,
  10. python后端和爬虫_【后端开发】python爬虫难学吗
  11. f5 系统损坏,重新安全系统
  12. 自动注册gmail邮箱构想
  13. CasADi——数据类型详解与基本操作介绍
  14. PIE-Engine 教程:水稻面积提取1(宿迁市)
  15. 安徽师大附中%你赛day9 T2 富 解题报告
  16. 数字人民币来了!它与支付宝、微信有什么区别吗?
  17. oracle 取记录最大的那条记录_相机记录高三备考生的一天,看哭万人: 读书虽苦,却是最容易的那条路!...
  18. (3.6A)不用库函数实现字符串拼接
  19. 这个 Go 开发的网络抓包工具,不仅好用还支持ES检索
  20. outlook使用笔记

热门文章

  1. 结合openCV学习DIP之传统图像特征与匹配
  2. linux 蓝牙设备,Ubuntu8.04下蓝牙设备连接管理
  3. 语义分割标签制作全过程(适合新手)
  4. linux根目录爆满解决方法
  5. 基于小波变换编码的纹理图像分割
  6. wincc脚本打印斑马打印机条码,斑马打印机接口
  7. 使用广播接收者获取短信及拦截电话
  8. 专访松下 | 20多年养老经验,日本养老标杆落地中国的经验与挑战
  9. win7 删除java_windows7系统卸载java的操作方法?
  10. java面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?