Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21612   Accepted: 10274   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

Source

题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行、每列、每个子网格内都只能使用一次1~9中的一个数字,即每行、每列、每个子网格内都不允许出现相同的数字,填完数独。

分析:直接搜索,标记行、列、块,值得一提的是倒着搜比正着搜效率高出许多,这也算是一个技巧。

代码:

 ////#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int t; int R()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
char s[][];
int a[][];
bool ok(int ans,int x,int y)
{
for(int i=;i<;i++)
if(a[i][y]==ans) return ;
for(int i=;i<;i++)
if(a[x][i]==ans) return ;
int xx=x-x%,yy=y-y%;
for(int i=xx;i<xx+;i++)
for(int j=yy;j<yy+;j++)
if(a[i][j]==ans) return ;
return ;
}
bool okk=;
void dfs(int x,int y,int cnt)
{
if(cnt==){
okk=;//满足条件后立刻结束,并标记
return;
}
while(a[x][y]){
if(y==) x++,y=;
else y++;
if(x==) {okk=;return;}//满足条件后立刻结束,并标记
}
for(int i=;i<=;i++){
if(ok(i,x,y)){
a[x][y]=i;
if(y==) dfs(x+,,cnt+);
else dfs(x,y+,cnt+);
if(okk) return;//满足条件后立刻结束
a[x][y]=;
}
}
return;
}
int main()
{
t=R();
while(t--)
{
int cnt=;
memset(a,, sizeof(a));
memset(s,, sizeof(s));
okk=;
for(int i=;i<;i++)
{
scanf("%s",s[i]);
for(int j=;j<;j++){
a[i][j]=s[i][j]-'';
if(!a[i][j]) cnt--;
}
}
dfs(,,cnt);//输出即为满足条件的,结束后的情况。
for(int i=;i<;i++){
for(int j=;j<;j++){
printf("%d",a[i][j]);
}
puts("");
}
}
}
 

最新文章

  1. js(jQuery)获取时间的方法及常用时间类
  2. 中小公司PMO不一样期间的责任
  3. C#二维数组
  4. Oulipo(poj 3461)
  5. 六款值得推荐的android(安卓)开源框架简介
  6. 关于RotateAnimation的各构造方法的参数
  7. Date与Calendar
  8. IDE idea 更换项目的JDK步骤
  9. Android 开源项目android-open-project工具库解析之(一) 依赖注入,图片缓存,网络相关,数据库orm工具包,Android公共库
  10. Android 关于 OnScrollListener 事件顺序次数的简要分析
  11. 3.if结构
  12. Python之缩进块
  13. Linux记录-lsof打开文件工具常用操作
  14. PLSQL基础学习-文字
  15. git上传到码云
  16. 【Spring Security】七、RememberMe配置
  17. AngularJS $scope 继承性 作用 生命周期
  18. OpenFeign封装为springboot starter
  19. sam9260 adc module
  20. 简单的PHP的任务队列

热门文章

  1. The ninth day
  2. Miner3D Basic基础版
  3. css3照片墙
  4. Assembly测试
  5. ASP.NET与json对象互转
  6. Spark核心组件
  7. S/4HANA for Customer Management里的搜索分页处理
  8. IOS 九宫格算法
  9. POJ-2503 Babelfish---map或者hash
  10. python 面向对象(四)--实例属性和类属性