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. 不经意间网易开源镜像去掉了FreeBSD的镜像

    http://mirrors.163.com/ FreeBSD已经到了这么不招人待见的地步了么? 网易曾经可是FreeBSD的大户啊.

  2. 无法打开Android SDK Manager的解决办法

    不知道从什么时候开始,打开Android的SDK Manager.exe时,命令行窗口一闪就自动关掉了. 想更新一些Android的东西都更新不了. 查了一下,解决办法是: 环境变量的系统变量Path ...

  3. [moka同学笔记]yii2.0查询数据库

      一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all()   二. [id=1]  选 ...

  4. git 使用笔记(一)

    1. 环境介绍 windows10 2.使用 2.1 安装git for windows 2.2 创建一个文件夹, 开始git管理 2.3 查看该目录,包括隐藏文件 2.4 把testgit.txt添 ...

  5. Verilog学习笔记设计和验证篇(一)...............总线和流水线

    总线 总线是运算部件之间数据流通的公共通道.在硬线逻辑构成的运算电路中只要电路的规模允许可以比较自由的确定总线的位宽,从而大大的提高数据流通的速度.各个运算部件和数据寄存器组可以通过带有控制端的三态门 ...

  6. spring mvc绑定复杂对象报错“Could not instantiate property type [com.ld.net.spider.pojo.WorkNode] to auto-grow nested property path: java.lang.InstantiationException: com.ld.net.spider.pojo.WorkNode”

    解决方法之一: 1.确保所有的Pojo都包含了默认的构造器:

  7. mariadb connector bug

    为了解决http://www.cnblogs.com/zhjh256/p/5807086.html的问题测试mariadb connector,常规的增删改查没有问题. 这货本来是为了解决存储过程bu ...

  8. Tomcat一些小事

    1.编码问题 1.1.乱码 客户端发请GET请求,如果这个请求地址上有中文,而且也没有进行encode的时候,后端就可能接收到乱码. --解决办法 在tomcat , conf/server.xml ...

  9. Force.com微信企业号开发系列(一) - 启用二次验证

    微信于9月份推出企业号后引起了业界不小的反响,许多企业都在思索企业号将如何影响企业的运营,从本文开始,我将详细阐述微信企业号开发的相关知识,而本文将着重介绍如何实现更高安全机制的二次验证. 申请企业体 ...

  10. 转 asp.net中如何退出整个框架(frameset),回到登录界面

    如: <frameset rows= "74,*,0,0 " cols= "* " frameborder= "NO " border ...