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. IP编址

    IP地址 /include/linux/inetdevice.h,定义IPV4专用的网络设备相关的结构.宏等 /net/ipv4/devinet.c.支持IPV4特性的设备操作接口 数据组织 net_ ...

  2. [转]Understanding Integration Services Package Configurations

    本文转自:http://msdn.microsoft.com/en-us/library/cc895212.aspx Introduction With the 2008 release, SQL S ...

  3. 向PE文件植入后门代码技术讨论

    写在前面的话 这篇文章将介绍使用codecaves对PE文件植入后门代码.有几个很好的工具可以帮到你了.比如BackdoorFactory和Shelter将完成相同的工作,甚至绕过一些静态分析几个防病 ...

  4. vim 多窗口,多tab编辑

    原文: https://blog.csdn.net/shuangde800/article/details/11430659 ------------------------------------- ...

  5. [Algorithom] Shuffle an array

    Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...

  6. 内网渗透技巧:判断机器真实外网IP的5种方法总结

    在内网渗透中有时需要在某台WEB服务器中留下后门,该机器可以通过内网IP建立IPC连接,但还需要获知外网IP或域名才能访问Wbshell,在无网关权限的情况下,我总结了有如下方法: 1.通过nsloo ...

  7. ffmpeg相关资源

    FFPLAY的原理(一) http://blog.csdn.net/shenbin1430/article/details/4291893 ubuntu12.04下命令安装ffplay等: sudo ...

  8. URL重写:RewriteCond指令与RewriteRule 指令格式

    Rewirte基本的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正則表達式规范.平时帮助我们实现拟静态,拟文件夹,域名跳转,防止盗链等.本文将针对mod_rewrite和URL匹配的技术 ...

  9. 【VBA编程】01.第一个VBA程序Hello world

    [程序1] 所有程序语言的开始都源于Hello world,那么我们也使用Hello world进行第一个VBA编程 新建Excle文件-----文件-------选项-----自定义功能区域---- ...

  10. 06-编写Hibernate API编写访问数据库的代码,使用Junit进行测试

    用到的注解: @Test:测试方法 @Before:初始化方法. @After:是否资源. 先执行Befere,然后执行Test,最后执行After. 第一步:新建一个Junit目录. 第二步:取名 ...