9 * 9 数独游戏小程序 (DFS)
来源:互联网 发布:数据散修txt下载 编辑:程序博客网 时间:2024/04/28 08:58
#include <iostream>#include <cstdio>#include <cstdlib>#include <string>#include <cmath>#include <algorithm>#include <cstring>#include <map>#include <set>#include <sstream>#include <queue>#include <stack>#define INF 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a));#define For(a,b) for(int i = a;i<b;i++)#define LL long long#define MAX_N 100010using namespace std;const int maxn = 100;//map<int,pair<int,int> > m; mx数组的功能不能够用map的映射代替//打印一下所记录的坐标就知道这样会少很多未知的位置bool r[maxn][maxn],c[maxn][maxn]; // 标记某行或者某一列是否含有某个数字int p[maxn][maxn]; // 记录数独矩阵int g; // 记录未知未知的个数int x[maxn],y[maxn]; // 记录所有未知位置的坐标的一个映射bool look_around(int u,int s){ int e = x[u] / 3 * 3; int r = y[u] / 3 * 3; for(int i = e; i<e+3; i++) { for(int j = r; j<r+3; j++) { if(p[i][j] == s) return false; } } return true;}bool dfs(int f){ if(f == g) return true; for(int i = 1; i<=9; i++) { if(!r[x[f]][i] && !c[y[f]][i] && look_around(f,i)) { r[x[f]][i] = true; c[y[f]][i] = true; p[x[f]][y[f]] = i; if(dfs(f+1)) return true; r[x[f]][i] = false; c[y[f]][i] = false; p[x[f]][y[f]] = 0; } } return false;}int main(){ while(1) { g = 0; memset(r,false,sizeof(r)); memset(c,false,sizeof(c)); cout<<"Please input the original matrix of sudoku :"<<endl<<endl; for(int i = 0; i<9; i++) { for(int j = 0; j<9; j++) { scanf("%d",&p[i][j]); if(p[i][j] == 0) { x[g] = i; y[g] = j; g++; } else { r[i][p[i][j]] = true; c[j][p[i][j]] = true; } } } dfs(0); cout<<endl<<endl; cout<<"The matrix of sudoku is :"<<endl<<endl; for(int i = 0; i<9; i++) { printf("%d",p[i][0]); for(int j = 1; j<9; j++) { printf(" %d",p[i][j]); } printf("\n"); } cout<<endl<<endl<<endl;// for(int i = 0; i<g; i++)// cout<<mx[i][0]<<' '<<mx[i][1]<<endl; } return 0;}/*2016/2/11测试样例:0 0 0 0 0 5 8 0 00 9 0 0 0 0 0 0 00 0 7 0 6 0 2 9 50 0 0 0 0 0 0 0 06 0 4 0 2 7 0 0 00 2 9 0 0 0 0 4 00 0 1 9 0 0 0 0 80 8 2 0 7 0 5 0 90 0 5 0 8 2 1 7 0*/
0 0
- 9 * 9 数独游戏小程序 (DFS)
- 数独游戏(leetcode) dfs 回溯
- 数独游戏程序
- 数独(DFS)
- 数独(DFS)
- hdu1426 Sudoku Killer(数独游戏)--DFS
- “数独”游戏-Java-dfs搜索算法
- hdu1426数独游戏Sudoku Killer DFS
- C语言小程序-(打印9*9的数独)
- poj2676数独(dfs)
- dfs(0634-数独)
- POJ2676数独(DFS)
- 数独游戏(完整版)
- 数独计算小程序开发(一)
- 数独(sudoku)游戏的程序求解
- 数独游戏的程序算法
- 计算数独的小程序-.-
- generate(粗糙) and solve(DFS) 数独(9*9)
- 栈、队列(自定义队列)、数组
- leecode 解题总结:79. Word Search
- redis类型说明及应用场景
- Meshlab源码初探
- UVA-11882 bfs + dfs + 剪枝
- 9 * 9 数独游戏小程序 (DFS)
- 1.九宫格闪动效果
- Ruby on Rails学习笔记(6)--Ruby中的变量
- NGUI的流光shader解决方案(一)(支持UITexture和使用图集的UISprite)
- MySQL支持的数据类型
- 蓝桥杯 Basic-2 01字串
- Android Fragment 真正的完全解析(上)
- CXF的拦截器(七)
- 浅谈static关键字