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. git操作-如何把你的本地仓库放到GitHub已有仓库

    本地: 首先在你的本地git init 初始化一个仓库,然后git add . 将所有的文件都打包到仓库中,git -commit -m "first_commit" 此时是没有什 ...

  2. Java Web Start

    1. JNLP 2. Security issue: https://java.com/en/download/help/win_controlpanel.xml Windows 7, Vista C ...

  3. RabbitMQ安装和介绍

    简单的安装方式 yum安装erlang,下载rpm包安装rabbitmq 一.编译安装erlang 1. 官方下载包并解压 wget http://erlang.org/download/otp_sr ...

  4. Linux 中权限的再讨论( 上 )

    前言 在Linux系统中,用户分为三个部分( 所有者 同组人 其他 ).每个部分的权限又可以赋予读/写/执行权限.这样,文件的权限标记一共包含 9 个权限位.好了,很多朋友对于Linux权限的了解就仅 ...

  5. IOS8 TouchID使用介绍

    本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123 IOS8将指纹识别技术开放出来了.我们能够利用用户设置的touch I ...

  6. SAM4E单片机之旅——6、LED闪烁之按钮控制

    现在试试用按钮控制LED灯……让LED在一个按钮按下时亮起:弹起时灭掉. 主要目的是学习GPIO的输入及中断. 一. 电路 图中的J39-n是几个跳线插座,位置在开发板LCD附近,往下进行前要先确保跳 ...

  7. sharding-jdbc源码学习(一)简介

    背景 对于大型的互联网应用来说,数据库单表的记录行数可能达到千万级甚至是亿级,并且数据库面临着极高的并发访问.采用Master-Slave复制模式的MySQL架构,只能够对数据库的读进行扩展,而对数据 ...

  8. echart地图下钻

    需求:展示中国地图,鼠标点击显示对应的省份 在echart的github上下载需要的 地图文件China.js,各个省份的json文件 遇到的问题:直接在浏览器打开报错,跨域问题,用webstrom打 ...

  9. 【智能无线小车系列十】通过USB摄像头实现网络监控功能

    如果仅有静态图像可能还不足以满足我们的需求,我们可能会需要用到实时的监控功能.这里介绍一款小应用:motion.motion的功能可强大了,不仅可以将监控的画面通过视频传输,实时展现,更为强大的是,m ...

  10. Vue数据双向绑定探究

    前面的啰嗦话,写一点吧,或许就有点用呢 使用过vue的小伙伴都会感觉,哇,这个框架对开发者这么友好,简直都要笑出声了. 确实,使用过vue的框架做开发的人都会感觉到,以前写一大堆操作dom,bom的东 ...