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. Java性能调优之:idea变慢解决

    今天搬砖的时候遇到一个问题,idea总是卡死,完全无法愉快的玩耍.幸好机器是Linux系统的.于是通过以下方式解决了问题: 通过top命令,查看系统运行状态发现4个CPU中有1个CPU用户占用率为10 ...

  2. java关键包简易说明

    java.lang 语言核心类,系统自动导入. java.util   java工具类.集合框架.时间,日历等. java.net   网络编程接口和类. java.io 流的接口和类 java.te ...

  3. (原创)RecyclerView结合xUtils2.6实现滚动时不加载item,xUtils2.6的源码分析与改造

    我们知道xUtils中的bitmapUtils与listview相配合可以实现滚动时暂停加载 只需要一句话: listview.addOnScrollListener(new PauseOnScrol ...

  4. 基于webapi的移动互联架构

    又到了一年最后一次上班了,写下这篇日志作为本年总结. 首先总体介绍一下项目背景,今年公司开发了一款app,本人一个人负责app的接口服务.微信开放平台搭建以及系统后台,上线半年,如今活跃用户数3W+. ...

  5. PHP生成日历数组,减少分页

    2016年11月14日 20:45:41 星期一 情景, 每个用户设置定投, app的屏幕比较小, 觉得常规的线性分页下拉不是很方便 吧列表改为日历格式的会更方便执行, 每年12个月, 总过12个页面 ...

  6. Cnblogs自定义皮肤css样式-星空观测者

    不知不觉来Cnblogs也这么久了,然而Blogs提供的主题还是依旧那么复古,总觉得阅读起来难免枯燥,虽然我认为做技术不可以太过浮躁,但是一个美观的主题终究是吸引人眼的第一要素. 毕竟这么久了,在博客 ...

  7. How to Fix GNOME License Not Accepted Issue on CentOS 7

    This post assume that you have just finished the Gnome GUI installation on CentOS 7 by using “yum gr ...

  8. ThinkPHP函数详解:U方法

    U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的URL地址,格式为:U('地址','参数','伪静态','是否跳转','显示域名');在模板中使用U方法而不是固定 ...

  9. Quartz.net Trigger触发器下 Cron表达式的格式

    有位博主写的不错,样式标准好理解,借鉴下. foamflower 1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] ...

  10. Linux 系统中堆栈的使用方法

    本节内容概要描述了Linux内核从开机引导到系统正常运行过程中对堆栈的使用方式.这部分内容的说明与内核代码关系比较密切,可以先跳过.在开始阅读相应代码时再回来仔细研究. Linux 0.12系统中共使 ...