Sudoku

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

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127 数独游戏。感觉这道题的代码还是蛮实用的~思路是dfs从头到尾依次枚举每个0点,尝试填入1->9,不合适再返回上层,直到填满0为止。
还有就是反搜(0ms),虽然我不会证明,但是和正搜(891ms)对比,是不是很神奇?
#include<stdio.h>
#include<string.h> char aa[][];
int a[][];
int row[][],col[][],squ[][];
int c1,c2,f; int jud(int x,int y)
{
if(x<=&&y<=) return ;
if(x<=&&<=y&&y<=) return ;
if(x<=&&<=y) return ;
if(<=x&&x<=&&y<=) return ;
if(<=x&&x<=&&<=y&&y<=) return ;
if(<=x&&x<=&&<=y) return ;
if(<=x&&y<=) return ;
if(<=x&&<=y&&y<=) return ;
if(<=x&&<=y) return ;
} void dfs()
{
int i,j,k;
if(f==) return;
if(c1==c2){
f=;
for(i=;i<=;i++){
for(j=;j<=;j++){ //反搜枚举9->1
printf("%d",a[i][j]);
}
printf("\n");
}
return;
}
for(i=;i<=;i++){
for(j=;j<=;j++){
if(a[i][j]==){
for(k=;k<=;k++){
if(row[i][k]==&&col[j][k]==&&squ[jud(i,j)][k]==){
row[i][k]=;
col[j][k]=;
squ[jud(i,j)][k]=;
a[i][j]=k;
c2++;
dfs();
row[i][k]=;
col[j][k]=;
squ[jud(i,j)][k]=;
a[i][j]=;
c2--;
}
}
return;
}
}
}
} int main()
{
int t,i,j;
scanf("%d",&t);
while(t--){
memset(a,,sizeof(a));
memset(row,,sizeof(row));
memset(col,,sizeof(col));
memset(squ,,sizeof(squ));
c1=;
for(i=;i<;i++){
getchar();
scanf("%s",aa[i]);
for(j=;j<;j++){
a[i+][j+]=aa[i][j]-'';
if(a[i+][j+]==) c1++;
else{
row[i+][a[i+][j+]]=;
col[j+][a[i+][j+]]=;
squ[jud(i+,j+)][a[i+][j+]]=;
}
}
}
c2=;f=;
dfs();
}
return ;
}

POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  2. POJ 2676/2918 数独(dfs)

    思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...

  3. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  4. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  5. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  6. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  7. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  8. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

  9. DFS POJ 2676 Sudoku

    题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...

随机推荐

  1. ipython notebook 如何打开.ipynb文件?

    标签: pythontensorflow 2017-03-29 14:17 235人阅读 评论(0) 收藏 举报  分类: TensorFlow(13)  转自:https://www.zhihu.c ...

  2. idea分支合并

    1.切换到指定分支(例如dev) 2.点击master的merge进行合并 注意:最好删掉本地的master和dev然后重新拉下远程的master和dev生成最新的本地master和dev

  3. linux下如何安装软件(转载)

    来源:http://zhidao.baidu.com/link?url=5oR8WxygPvVMhSZvXQahYKm01JPTmQnEUjbQF562Yxgd3r6bYpki1ZPcHAsij6E4 ...

  4. 一段经典的 Java 风格程序 ( 类,包 )

    前言 本文给出一段经典的 Java 风格程序,请读者初步体会 Java 和 C++ 程序的不同. 第一步:编写一个类 // 将这个类打包至 testpackage 包中 package testpac ...

  5. java中 hashCode() 和 equals()

    1. 值类型是存储在内存中的栈,而引用类型的变量在栈中仅仅是存储引用类型变量的地址来自堆,而其本身则存储在栈中. 2. ==操作比较的是两个变量的值是否相等, 3. 对于引用型变量表示的是两个变量在堆 ...

  6. 电脑插入U盘后显示CD驱动器,如何还原为正常U盘?

    以前用电脑店制作U盘启动工具,结果U盘的一小部分变成了CD驱动器, 无论怎么格式化都无法将U盘还原,这几天心血来潮,看了一些恢复U盘的教程, 打算写一篇博客讲解一下还原过程(本人亲测) 1.下载Chi ...

  7. Oracle 11gR2 使用RMAN Duplicate复制数据库

    Oracle 11gR2 使用RMAN Duplicate复制数据库     前言:     上周刚做完一个项目,用户要求RAC的数据库可以自己主动备份到另外一个单节点上,单节点可以正常拿起来就能用. ...

  8. Ext rowcontextmenu回调

    今天栽了大跟头,,,, EXT js右键可以获取到选中的那一列的索引值,只需在回调函数中取到那一列的数据就可以ok,不用再费尽心思的费用执行click的事件 grid.on("rowcont ...

  9. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  10. -webkit-mask-box-image给框架加个同样大小的遮罩

    很棒的css属性,可以在div上方建个同大小的遮罩,配合线性变化gradient可以实现很酷的样式,比如:时间选择的UI组件,里面有个模糊的上方遮罩 手册地址:https://developer.mo ...