Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13665   Accepted: 6767   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
讲解:游戏都玩过,但就是基本上见过没做出来过,哈哈、、、挺有意思的代码,题意都知道,关键就是如何进行搜索,其实就是不断地进行枚举,如果满足所有的条件就跳出来;
AC代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
int Map[N][N], flag;
char MAp[N][N];
int check(int ans,int key)
{
int x = (key-)/+;
int y = key-(x-)*;
for(int i=; i<=; i++)//行是否冲突
if(Map[x][i] == ans)
return ;
for(int i=; i<=; i++) //列是否冲突
if(Map[i][y] == ans)
return ;
if(x<=)x = ; //x所在小九格起点
if(x> && x<)x=;
if(x>=) x=;
if(y<=) y = ; //y所在小九格起点
if(y> && y<) y=;
if(y>=) y=;
for(int i=; i<; i++) //小的九格是否冲突
for(int j=; j<; j++)
if(Map[x+i][y+j] == ans)
return ;
return ;//以上条件都不符合,返回1;
}
int dfs(int key)
{
int x = (key-)/+;
int y = key-(x-)*;
if(key>)
{
flag = ;
return ;
}
if(Map[x][y]!=)//不是0,直接跳过去
{
dfs(key+);
}
else //否者,在这个位置枚举九个数,进行递归搜索
{
for(int k=; k<=; k++)
if(check(k,key))
{
Map[x][y] = k;
dfs(key+);
if(flag == ) return ;
Map[x][y] = ;
}
}
return ;
}
int main()
{
int T,i,j;
// freopen("in2.txt","r",stdin);
// freopen("out2.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
memset(Map,,sizeof(Map));
for(i=; i<=; i++)
for(j=; j<=; j++)
{
cin>>MAp[i][j];
Map[i][j] = MAp[i][j]-'';
}
flag = ;
dfs();
for(i=; i<=; i++)
{
for(j=; j<=; j++)
printf("%d",Map[i][j]);
printf("\n");
}
}
return ;
}

poj Sudoku(数独) DFS的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

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

  2. POJ Sudoku 数独填数 DFS

    题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   Sp ...

  3. POJ 2676 数独(DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21612   Accepted: 10274   Specia ...

  4. POJ 2676 数独+dfs深搜

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

  5. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. POJ.3172 Scales (DFS)

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

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

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

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

  9. 【POJ - 2676】Sudoku(数独 dfs+回溯)

    -->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...

随机推荐

  1. Unity3D 手游开发中所有特殊的文件夹

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  2. Windows环境下32位汇编语言程序设计(典藏版)

    <Windows环境下32位汇编语言程序设计(典藏版) > 基本信息 作者: 罗云彬 出版社:电子工业出版社 ISBN:9787121207594 上架时间:2013-7-8 出版日期:2 ...

  3. julia,集Python、C++、R为一体!Julia 1.0重磅发布, MIT发布史上最强科学计算编程语言?创始人独家解答11个问题

    这个编程语言的新版本之所以受到整个人工智能界的关注,最主要的原因正是其将 C 语言的速度.Ruby 的灵活.Python 的通用性前所未有地结合在一起,支持并行处理,易于学习和使用,尤其适合科学和工程 ...

  4. Javascript中的对象(二)

    Javascript是一种基于原型的对象语言,而不是我们比较熟悉的,像C#语言基于类的面向对象的语言.在前一篇文章中,我们已经介绍了Javascript中对象定义的创建.接下来我们来介绍一下Javas ...

  5. less、sass、stylus

    less.sass.stylus 它们是三种类似的样式动态语言,属于css预处理语言,它们有类似css的语法,为css赋予了动态语言的特性.如变量.继承.运算.函数等.这么做是为了css的编写和维护. ...

  6. 分析apache日志,统计ip访问频次命令

    统计访问频次最高的10个ip: cat /var/log/httpd/access_log |awk '{print $1}'|sort|uniq -c|sort -nr|head -10 统计恶意i ...

  7. 【转】编译安装PHP并配置PHP-FPM

    1.前言上一篇讲述了如何编译安装MySQL,虽然可以通过yum install 或者rpm来安装,但是yum install和rpm安装有一个特点,就是有些参数是别人根据大众需求定制的,如果需要进行自 ...

  8. 云计算之路-试用Azure:数据库备份压缩文件在虚拟机上的恢复速度测试

    测试环境:Windows Azure上海机房,虚拟机配置为大型(四核,7 GB 内存),磁盘情况见下图. 数据库备份压缩文件大于为12.0 GB (12,914,327,552 bytes),放置于T ...

  9. 算法笔记_115:算法集训之代码填空题集二(Java)

     目录 1 连续数的公倍数 2 孪生素数 3 迷宫走法 4 拍7游戏 5 排列为平方数 6 平面点最小距离 7 扑克牌排列 8 三进制转十进制 9 识别复制串 10 蔬菜价格计算   1 连续数的公倍 ...

  10. java设计模式之模板方法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744002 今天你还是像往常一样来上班,一如既往地开始了你的编程工作. 项目经理告 ...