POJ 2676 Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17627   Accepted: 8538   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
我的思路错误之处:我本来只设置了两个二维bool数组表示每行9个数字和每列9个数字,是否用过,再深搜填写9个方格,可是发现,深搜填写每个方格是很难写的。
正解:开是三个二维bool数组,分别表示每列每行每个大方格的数字的使用情况。
读入时注意数字是连起来的。
 /*----------------正确代码-------------------------*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 15
char duru[N];
int jz[N][N];
bool flag=false,flagfg[N][N],flaghang[N][N],flaglie[N][N];
inline void input()
{
for(int i=;i<=;++i)
{
scanf("%s",duru+);/*注意读入的数字之间没有空格*/
for(int j=;j<=;++j)
{
jz[i][j]=duru[j]-'';
if(jz[i][j]==) continue;
int k=*((i-)/)+(j-)/+;
flagfg[k][jz[i][j]]=true;
flaghang[i][jz[i][j]]=true;
flaglie[j][jz[i][j]]=true;
}
}
}
void output()
{
for(int i=;i<=;++i)
{
for(int j=;j<=;++j)
printf("%d",jz[i][j]);
printf("\n");
}
}
void dfs(int x,int y)
{
if(x==)
{
flag=true;
return;
}
if(jz[x][y])
{
if(y==)
dfs(x+,);
else dfs(x,y+);
if(flag) return;
}
else {
int k=*((x-)/)+(y-)/+;
for(int i=;i<=;++i)
{
if(!flaghang[x][i]&&!flaglie[y][i]&&!flagfg[k][i])
{/*枚举符合三个条件的i*/
jz[x][y]=i;
flaghang[x][i]=true;
flaglie[y][i]=true;
flagfg[k][i]=true;
if(y==)
dfs(x+,);
else dfs(x,y+);
if(flag) return;
jz[x][y]=;/*回溯*/
flaghang[x][i]=false;
flaglie[y][i]=false;
flagfg[k][i]=false; }
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
input();
dfs(,);
output();
memset(jz,,sizeof(jz));
memset(flagfg,false,sizeof(flagfg));
memset(flaghang,false,sizeof(flaghang));
memset(flaglie,false,sizeof(flaglie));
flag=false;
}
return ;
}

深搜+回溯 POJ 2676 Sudoku的更多相关文章

  1. HDU5723 Abandoned country (最小生成树+深搜回溯法)

    Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...

  2. ****Curling 2.0(深搜+回溯)

    Curling 2.0 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  3. ACM : POJ 2676 SudoKu DFS - 数独

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

  4. The 2016 ACM-ICPC Asia China-Final L World Cup(深搜+回溯 暴力求解)

    题目分析: 对于A,B,C,D四支队伍,两两之间进行一场比赛,获胜得3分,平局得1分,失败不得分,现在对给出的四个队伍的得分,判断能否满足得到这种分数,且方案唯一输出yes,不唯一输出no,不可能则输 ...

  5. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  6. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  7. 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 ...

  8. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

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

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

随机推荐

  1. Urlencode and Urldecode 命令行

    由于经常使用,简单记录之 $ alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.ar ...

  2. 使用layout_weight设置控件占屏幕百分比

    水平LinearLayout中如果A,B两个控件都是layout_weight="1",那么控件在水平方向占比为A的layout_width+1/2空闲空间,B的layout_wi ...

  3. php获取textarea的值并处理回车换行的方法

    //注:\n是用双引号包的的,双引号!!双引号!!! explode("\n",$row[0]['value']

  4. 二、SQL语句映射文件(1)resultMap

    //备注:该博客引自:http://limingnihao.iteye.com/blog/106076 SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对 ...

  5. SQL Server性能影响的重要结论

    第一次访问数据会比接下来的访问慢的多,因为它要从磁盘读取数据然后写入到缓冲区: 聚合查询(sum,count等)以及其他要扫描大部分表或索引的查询需要大量的缓冲,而且如果它导致SQL Server从缓 ...

  6. mysql并发insert deadlock分析以及解决,无delete/update/for update

    关于并发insert操作发生deadlock这个情况,一直有很多争议,而且网上的帖子所有的例证和模拟其实不一定反映了真实的情况,例如:https://www.percona.com/blog/2012 ...

  7. margin和padding对行内元素的影响

    这个是在面试的时候,面试官问我的一个小问题 自己没有考虑过inline元素设置margin和padding的问题 学习的过程记录下来 1)inline元素的高度是由元素的内容决定的(字体的大小和行高) ...

  8. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

  9. SharePoint 2013 设置自定义布局页

    在SharePoint中,我们经常需要自定义登陆页面.错误页面.拒绝访问等:不知道大家如何操作,以前自己经常在原来页面改或者跳转,其实SharePoint为我们提供了PowerShell命令,来修改这 ...

  10. C++非类型模板参数

    对于函数模板与类模板,模板参数并不局限于类型,普通值也可以作为模板参数.在基于类型参数的模板中,你定义了一些具体的细节来加以确定代码,直到代码被调用时这些细节才被真正的确定.但是在这里,我们面对的是这 ...