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("");
}
}
}
 

POJ 2676 数独(DFS)的更多相关文章

  1. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  2. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  3. 随手练——POJ - 2676 数独 (回溯法)

    POJ - 2676 : http://poj.org/problem?id=2676: 解题思想 (大力出奇迹): 1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行.列,以及3 ...

  4. poj 2676 数独问题 dfs

    题意:完成数独程序,数独要求每行每列且每个3*3矩阵都必须是1~9的数字组成. 思路:dfs 用row[i][n] 记录第i行n存在  用col[j][n] 记录第j列n存在 grid[k][n] 记 ...

  5. poj 2676 Sudoku ( dfs )

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

  6. 深搜+回溯 POJ 2676 Sudoku

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

  7. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  8. POJ 2676 Sudoku (数独 DFS)

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

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

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

随机推荐

  1. webpack打包将配置文件单独抽离不压缩打包

    webpack.config.js: plugins: [ //提取公共模块 new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', ch ...

  2. HTML超链接实用

    1.文本链接: <a href="http://www.meng.com/" target="_blank">访问meng!</a> 2 ...

  3. Implementation with Java

    Implementation with Java From:http://jcsc.sourceforge.net In general, follow the Sun coding conventi ...

  4. Android TextView之空格占位法

    在Android布局中进行使用到空格,为了实现文字的对齐.具体要怎么使用了? •请忽视文中‘& #160’中&和#之间的空格 空格: & #160; 窄空格: & #8 ...

  5. 如何领域驱动设计?-实践感悟&总结分享

    主要是在开发过程中,个人对于领域驱动设计的实践感悟和总结:也是对新进开发人员的培训资料:希望对关注DDD的童鞋有所帮助. 概述 领域驱动不是纯粹的技术问题,领域建模(建立数据表只是一部分)是领域专家( ...

  6. zabbix3.0问题及解决方法

    一.问题:Zabbix agent on T2 is unreachable for 5 minutes         解决:1.进入zabbix service端 vim /etc/zabbix/ ...

  7. 笨办法学Python(五)

    习题 5: 更多的变量和打印 我们现在要键入更多的变量并且把它们打印出来.这次我们将使用一个叫“格式化字符串(format string)”的东西. 每一次你使用 " 把一些文本引用起来,你 ...

  8. 起一个node服务

    使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...

  9. 【转】git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚

    转载自:http://m.blog.csdn.net/blog/lihongli528628/45483463 [git 删除本地分支] git branch -D br [git 删除远程分支] g ...

  10. Qlikview 数据加载方法罗列

    以下是通常会用到的数据加载的方法,供大家参考: 1. 从文件加载: Data: Load *,RowNo() as InputKey; SQL SELECT ID,TEST,DATECREATED F ...