数独游戏的简单回溯解法
2015-11-18 23:28
465 查看
今天哥们儿突然让我帮着解个数独,一开始以为只要循环遍历就可以了,后来想想有些太麻烦,想着自己以前写过八皇后问题,研究了一下,还真弄出来了,高兴!
2 0 6 0 0 1 0 8 0
1 7 0 0 0 9 0 6 0
0 0 0 4 6 7 0 0 0
6 1 0 0 4 0 8 0 0
0 0 2 0 0 0 3 0 0
0 0 5 0 0 0 0 9 6
0 0 0 2 1 5 0 0 0
0 3 0 6 9 4 0 2 8
0 2 0 7 0 0 6 0 5
#include <iostream> #include <fstream> int num[9][9]; int posX[81]= {0}; int posY[81]= {0}; int resCount=0; using namespace std; void print() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { cout<<num[i][j]<<" "; } cout<<endl; } } void recordPos() //记录还没有添加数字的位置 { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(num[i][j]==0) { posX[resCount]=i; posY[resCount]=j; resCount++; } } } } bool checkZone(int x,int y)//检测该数在自己3*3的方格是否合法 { int tmpX = 3*(x/3); int tmpY = 3*(y/3); for(int i=tmpX; i<tmpX+3; ++i) { for(int j=tmpY; j<tmpY+3; ++j) { if(x==i&&j==y)continue; if(num[x][y]==num[i][j])return false; } } return true; } bool check(int step) //检测是否合法 { int x=posX[step]; int y=posY[step]; for(int i=0; i<9; i++) //行重复 { if(num[x][y]==num[x][i]&&y!=i)return false; } for(int i=0; i<9; i++) //列重复 { if(num[x][y]==num[i][y]&&x!=i)return false; } if(!checkZone(x,y))return false; return true; } void f(int step) { if(step==resCount) { print(); return ; } for(int i=1; i<=9; i++) { num[posX[step]][posY[step]]=i; if(check(step)) { f(step+1); } num[posX[step]][posY[step]]=0; } } int main() { freopen("in.txt","r",stdin); for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { cin>>num[i][j]; } } recordPos(); f(0); return 0; }测试用例:
2 0 6 0 0 1 0 8 0
1 7 0 0 0 9 0 6 0
0 0 0 4 6 7 0 0 0
6 1 0 0 4 0 8 0 0
0 0 2 0 0 0 3 0 0
0 0 5 0 0 0 0 9 6
0 0 0 2 1 5 0 0 0
0 3 0 6 9 4 0 2 8
0 2 0 7 0 0 6 0 5
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息