POJ 2676 数独 搜索
来源:互联网 发布:大数据的真正意义是 编辑:程序博客网 时间:2024/06/05 20:53
Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16386 Accepted: 8004 Special Judge
Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
Input
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
Sample Input
1103000509002109400000704000300502006060000050700803004000401000009205800804000107
Sample Output
143628579572139468986754231391542786468917352725863914237481695619275843854396127
Source
Southeastern Europe 2005
题意:给9个3*3的小正方形,使得小正方形里恰好有9个不重复的1--9,而且大正方形行或列不能有重复的数字。
题解:不太容易的搜索,开始根本没思路,还是看了课件的代码理解了一遍才开始敲得。说一下思路吧。开二维数组col,row分别记录大正方形所有的行,列数字出现情况,block记录9个小正方形的数字出现情况,主要是判断重复,加快搜索速度。这里给出的小矩阵的计算公式为(r/3)*3+c/3,可以表示为第几个矩阵。
这里我们同样要用到递归的思想,先标记为访问过,结束递归再恢复状态,所有的点都被赋予值则可以判断矩阵构造好了。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define CLR(x) memset((x),0,sizeof(x));#define N 20char ss[N][N];int g[N][N],col[N][N],row[N][N],block[N][N];struct point{int c,r;point (int _r,int _c):r(_r),c(_c){}point(){}};vector<point>eg;inline int get_block(int r,int c){r/=3;c/=3;return (r*3+c);}inline void SET(int r,int c,int num,int n){col[c][num]=block[get_block(r,c)][num]=row[r][num]=n;}inline bool judge(int r,int c,int num){return(!col[c][num])&&(!row[r][num])&&(!block[get_block(r,c)][num]);}bool dfs(int n){if(n<0){return true;}int r=eg[n].r;int c=eg[n].c;for(int i=1;i<=9;i++){if(judge(r,c,i)){g[r][c]=i;SET(r,c,i,1);if(dfs(n-1))return true;//在这里构造成功会不断跳出递归SET(r,c,i,0);}}return false;}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifint t;scanf("%d",&t);while(t--){eg.clear();CLR(g);CLR(col);CLR(row);CLR(block);for(int i=0;i<9;i++){scanf("%s",ss[i]);}for(int i=0;i<9;i++){for(int j=0;j<9;j++){if(ss[i][j]-'0'==0){eg.push_back(point(i,j));}else{g[i][j]=ss[i][j]-'0';SET(i,j,ss[i][j]-'0',1);}}}if(dfs(eg.size()-1)){for(int i=0;i<9;i++){for(int j=0;j<9;j++){printf("%d",g[i][j]);}printf("\n");}}}return 0;}
0 0
- POJ 2676 数独 搜索
- POJ 2676 Sudoku (数独 搜索)
- POJ 2676 数独(基础搜索)
- 搜索 --- 数独求解 POJ 2676 Sudoku
- POJ 2676 数独
- 简单搜索(数独)poj 2676 Sudoku
- POJ 2676(数独求解器 搜索)
- POJ 2676 Sudoku 数独
- poj 2676 数独 ---dfs
- POJ 2676 Sudoku (数独)
- 数独 Sudoku poj 2676
- POJ 2676:Sudoku 数独
- POJ 2676 && POJ 2918 数独 DFS
- 搜索函数 数独
- 数独 (搜索)
- 搜索--数独
- 数独 搜索
- POJ 2676 Sudoku 数独游戏
- Facecat的iOS自学笔记
- [POJ 1330]Nearest Common Ancestors[LCA](O(dep[u] + dep[v]))
- AngularJS实现跨域请求
- 二叉树的构建与遍历(链表法)
- 虚拟机 的 网络配置
- POJ 2676 数独 搜索
- PrefabType 预置类型
- Android 源码编译 指定userdata.img、system.img、cache.img容量大小
- 8,石头,剪刀,布的逻辑代码
- CvgTalk-Lulu-Periocular Recognition
- 【reversing.kr】ImagePrc 逆向分析
- Animation动画
- SQL——Procedure & Function
- 02Command对象的使用