Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
/*/
数独 AC代码:
/*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"queue"
#include"string"
using namespace std;
int map[11][11],count,n;
bool F[11][11];
bool H[11][11];
bool L[11][11]; struct Node {
int x,y;
} node[100]; int MAP(int i,int j) {
if(i<=3&&j<=3)return 1;
else if(i<=3&&j<=6)return 2;
else if(i<=3&&j<=9)return 3;
else if(i<=6&&j<=3)return 4;
else if(i<=6&&j<=6)return 5;
else if(i<=6&&j<=9)return 6;
else if(i<=9&&j<=3)return 7;
else if(i<=9&&j<=6)return 8;
else if(i<=9&&j<=9)return 9;
} int DFS(int n) {
if(n>count) {
return 1;
}
for(int j=1; j<=9; j++) {
if(!H[node[n].x][j]&&!L[node[n].y][j]&&!F[MAP(node[n].x,node[n].y)][j]) {
H[node[n].x][j]=L[node[n].y][j]=F[MAP(node[n].x,node[n].y)][j]=1;
map[node[n].x][node[n].y]=j;
if(DFS(n+1))return 1;
H[node[n].x][j]=L[node[n].y][j]=F[MAP(node[n].x,node[n].y)][j]=0;
}
}
return 0;
} int main() {
int flag=0;
while(cin>>n) {
for(int i=1; i<=n; i++) {
memset(map,0,sizeof(map));
memset(H,0,sizeof(H));
memset(F,0,sizeof(F));
memset(L,0,sizeof(L));
count = 0;
for(int j=1; j<=9; j++)
for(int k=1; k<=9; k++) {
scanf("%1d",&map[j][k]);
if(map[j][k]==0) {
count++;
node[count].x=j;
node[count].y=k;
} else {
H[j][map[j][k]]=1;
L[k][map[j][k]]=1;
F[MAP(j,k)][map[j][k]]=1;
}
}
DFS(1);
for(int j=1; j<=9; j++) {
for(int k=1; k<=9; k++)
cout<<map[j][k];
cout<<endl;
} }
}
return 0;
}

ACM : POJ 2676 SudoKu DFS - 数独的更多相关文章

  1. poj 2676 Sudoku ( dfs )

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

  2. 深搜+回溯 POJ 2676 Sudoku

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

  3. POJ 2676 Sudoku (数独 DFS)

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

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

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

  5. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

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

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

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  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. 【splay】文艺平衡树 BZOJ 3223

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  2. 设置html title标题左侧的小图标

    网页title旁边的小图片设置,图片要求格式必须是.ico,可以使用在线的转换工具把jpg和png图片转换为ico图片,工具地址:http://www.ico.la/ 在html文件中的<hea ...

  3. jquery.extend

    经常在插件中看到jquery.extend 方法,最近在尝试写一些简单的插件,顺便研究一下这个方法. 原文:http://www.cnblogs.com/RascallySnake/archive/2 ...

  4. VO对象和PO对象的区别

    VO,值对象(Value Object),PO,持久对象(Persisent Object),它们是由一组属性和属性的get和set方法组成.从结构上看,它们并没有什么不同的地方.但从其意义和本质上来 ...

  5. python 登录小程序

    运行程序前,需在本目录增加黑名单文件 # Author:JinYu # -*- coding:utf-8 -*- #登录3次锁定 local_username = 'jinyu' local_pass ...

  6. gulp使用过程中出现的问题

    在使用gulp的过程中,最容易出现错误的地方就是在安装本地的gulp的时候,错误的原因有: 1.本来是局部安装gulp,但使用命令时还带-g. 2.忘记在局部安装gulp. 以上两种情况出错时会报错, ...

  7. iOS-上架APP之启动页设置(新手必看!)

    今天自己做的小作品准备提交,就差一个启动页,各种百度,各种搜,结果还好最后终于出来了,和大家分享一下,这个过程中遇到的各种小问题.(注XCode版本为7.2) 1.启动页一般都是图片,因为苹果有4,4 ...

  8. 那些年,坑死自己的事之fread/fwrite

    今天继续看牛人做过的东西,这个小程序并不大,加上相当多的注释行,才5000多行.这个小程序是在linux下实现的,之前自己也一直用vi来看并加以更加详细的注释,但是效率实在太低.于是将其转移到wind ...

  9. Python学习笔记(3)

      1.元组 元组的定义符号是() ,元素定义与列表完全一致.不同的是元组的内容是不可变的. 2.字典 字典里面的内容是无序的. 字典的元素组成形式是 key:value key的定义规则:key是不 ...

  10. Launching web on MyEclipse Tomcat 问题

    错误提示: Launching web on MyEclipse Tomcat has encountered a problemAn internal error occurred during: ...