Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18105   Accepted: 8772   Special Judge

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 暑假艾教教的数独,留个板子。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int sudoku[][];
int column[][];
int row[][];
int nine[][];
struct node
{
int x,y;
};
node ready[];
int T;
int num = ;
char s[][];
int cal(int x,int y)
{
return (x-)/*+(y-)/+;
}
bool cmp(node A,node B)
{
return A.y>B.y;
}
void put(int x,int y,int tt,int flag)
{
if(flag) sudoku[x][y] = tt;
row[x][tt] = flag;
column[y][tt] = flag;
nine[cal(x,y)][tt] = flag;
}
int ok = ;
int can(int x,int y,int tt)
{
if(row[x][tt]) return ;
if(column[y][tt]) return ;
if(nine[cal(x,y)][tt]) return ;
return ;
}
void dfs(int k)
{
if(ok) return;
if(k==num+)
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(j==) printf("%d\n",sudoku[i][j]);
else printf("%d",sudoku[i][j]);
}
}
ok = ;
return;
}
for(int nex=;nex<=;nex++)
{
if(can(ready[k].x,ready[k].y,nex))
{
put(ready[k].x,ready[k].y,nex,);
dfs(k+);
put(ready[k].x,ready[k].y,nex,);
}
}
}
int main()
{
cin>>T;
while(T--)
{
ok = ;
memset(sudoku,,sizeof(sudoku));
memset(ready,,sizeof(ready));
memset(column,,sizeof(column));
memset(row,,sizeof(row));
memset(nine,,sizeof(nine));
num = ;
for(int i=;i<=;i++)
{
scanf("%s",s[i]+);
for(int j=;j<=;j++)
{
if(s[i][j]=='')
{
num++;
ready[num].x = i;
ready[num].y = j;
}
else
{
put(i,j,s[i][j]-'',);
}
}
}
sort(ready+,ready+num+,cmp);
dfs();
}
return ;
}

 

POJ Sudoku 数独填数 DFS的更多相关文章

  1. 第七届C/C++B-方格填数 DFS

    方格填数 如下的10个格子    +--+--+--+    |  |  |  | +--+--+--+--+ |  |  |  |  | +--+--+--+--+ |  |  |  | +--+- ...

  2. 第七届 蓝桥杯 方格填数 dfs

    如下的10个格子  填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数. 注意:你提交的应该是一个整数,不要填写任何 ...

  3. 梦工厂实验室 蛇形填数 dfs

    问题 D: 蛇形填数 时间限制: 3 Sec  内存限制: 64 MB提交: 28  解决: 5[提交][状态][讨论版] 题目描述 在n*n方阵里填入1,2,...,n*n,要求填成蛇形.例如n=4 ...

  4. 蓝桥杯 方格填数 DFS 全排列 next_permutation用法

    如下的10个格子(参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数.注意:你提交的应该是一个 ...

  5. POJ 2676:Sudoku 数独

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15830   Accepted: 7737   Special ...

  6. DFS(深度优先搜索遍历求合格条件总数)--07--DFS--蓝桥杯方格填数

    此题方法多种,我用规范的DFS来求解 题目:方格填数 如下的10个格子,填入0~9的数字.要求:连续的两个数字不能相邻. (左右.上下.对角都算相邻)一共有多少种可能的填数方案?   输出 请填写表示 ...

  7. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...

  8. java实现第三届蓝桥杯方块填数

    方块填数 "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6的小格被分为6个部 ...

  9. nyoj 33 蛇形填数

    蛇形填数 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 ...

随机推荐

  1. IntelliJ IDEA 7.0 正式版注册机代码

    好神奇,第一次看见注册机的源代码,自己运行一下.可以是java IDE环境,也可以是配置好jdk的dos环境. 然后输入自己的用户名就可以获得相对应的注册码,输入到软件中即可. 仅供参考,请购买正版. ...

  2. 开源日志系统比较:scribe、chukwa、kafka、flume

    1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...

  3. iOS打包app发给测试人员测试

    说明:在项目开发过程中经常需要开发人员将项目打包成ipa包后,发给测试人员进行测试.本文贴图对打包的过程简单介绍. 一.Product ->archive (注意,不能是模拟器状态,如果当前调试 ...

  4. jquery多级下拉菜单

    var menu = new Click('#menu',{target:'p',parent:'li',contr:'ul',way:0}); /* 参数说明: target : 点击事件发生在该元 ...

  5. 手机版WEB开发经验分享,手机版网站开发注意事项,网站自适应,手机版网站自适应,移动安卓APP自适应

    转自 http://my.oschina.net/cart/blog/282477 做前端开发不短了,用过jQuery Mobile jqMobi 也纯手工写过.. 最后总结如下: jQuery Mo ...

  6. 在 .NET 4 中使用托管可扩展性框架构建可组合的应用程序

    https://msdn.microsoft.com/zh-cn/library/ff576068.aspx http://blogs.msdn.com/b/nblumhardt/archive/20 ...

  7. PHP中GD库安装

    安装gd库扩展不能像其他扩展安装一样,直接./configure --prefix=/xxx 还需要激活png,jpeg,字库等支持 ./configure --prefix=/xxx --with- ...

  8. 解决在IIS中调用Microsoft Office Excel组件后进程无法正常退出的问题

    来源:http://www.cnblogs.com/ahui/archive/2013/03/05/2944441.html 有一个项目用到Excel组件产生报表,本以为这个通用功能是个很简单的cas ...

  9. C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)

    一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...

  10. List<T> 求差集

    List<, , , , , }; List<, , , , , }; List<int> c = b.Except(a).ToList(); foreach (int i i ...