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. Ubuntu 15.10

    安装Ubuntu 15.10后要做的事 http://blog.csdn.net/skykingf/article/details/45267517 ubuntu15.10 install-mac-t ...

  2. js json按key值排序

    最近有个需求需要把json按key值进行排序,可是js并没有直接的函数可以对json进行排序的这么办呢? 然后想到了一个间接的方法来实现: 1.将json中的key值取出,存在一个数组中,然后对这个数 ...

  3. MonoTouch.Dialog简介

    新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...

  4. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

  5. 【BZOJ4296】[PA2015]Mistrzostwa BFS

    [BZOJ4296][PA2015]Mistrzostwa Description 给定一张n个点m条边的无向图,请找到一个点数最多的点集S,满足:1.对于点集中任何一个点,它至少与d个点集中的点相邻 ...

  6. Spring mvc接受集合类型参数的方法

    public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...

  7. cerery

    cerery http://docs.celeryproject.org/en/latest/userguide/index.html

  8. 谈谈EJB是怎样公布Web Service的

    定义 我们常常会听到.xx项目中用到了Web Service.那么.什么是Web Service呢? 首先让我们来了解一下Web Service.Web Service技术.就是能使得执行在不同机器上 ...

  9. iOS反射机制:objc_property_t的使用

    #import <objc/runtime.h> 需要导入这个头文件. 动态获取一个自定义类对象中的所有属性 - (NSDictionary *)allProperties { NSMut ...

  10. Ext rowcontextmenu回调

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