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. js限制input输入

    1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...

  2. jquery ajax 用 data 和 headers 向 java RESTful 传递参数区别

    jquery 的 ajax 是非常方便的一个函数,记录一下 $.ajax 生成的 http 报文 一.使用 data 传递参数: $.ajax({ url : "webrs/test/add ...

  3. Java的集合框架

    01.为什么要使用集合框架? 解析:如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象,那么可以使用Java集合框架. 如果启用集合的删除方法,那么集合中所有元素的索引会自动维护. 集合 ...

  4. Durandal介绍

         Durandal是一个JS框架用于构建客户端single page application(SPAs).它支持MVC,MVP与MVVM前端构架模式.使用RequireJS做为其基本约定层,D ...

  5. [转载]拜占庭问题深入讨论 from http://bitkan.com/news/topic/14011

    拜占庭将军问题深入探讨 了解过比特币和区块链的人,多少都听说过拜占庭将军问题,或听说过比特币(或区块链)的一个重要成就正是解决了拜占庭将军问题.但真正明白这个问题的人并不多,甚至知道这个问题实质的人都 ...

  6. java微信开发(wechat4j)——设置响应微信参数

    设置响应微信参数 wechat4j框架官方文档: https://github.com/sword-org/wechat4j/wiki

  7. 2013 最新的 play web framework 版本 1.2.3 框架学习文档整理

    Play framework框架学习文档 Play framework框架学习文档 1 一.什么是Playframework 3 二.playframework框架的优点 4 三.Play Frame ...

  8. js封装tab标签页

    <html> <head> <title></title> <meta charset="UTF-8"> <sty ...

  9. Google Developers中国网站

    正于北京举办的谷歌开发者大会上,谷歌宣布,Google Developers中国网站 (developers.google.cn) 正式发布! 谷歌表示,Google Developers中国网站是特 ...

  10. ng-show

    //当ng-show="false"时,自动添加 #animate.ng-hide { } #animate.ng-hide-add { } #animate.ng-hide-ad ...