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. 用python生成基于lombok 和 hibernate 生成javabean

    mysql工具类 import pymysql.cursors import sys from contextlib import contextmanager import traceback im ...

  2. [Linux] ubuntu 安装 Wireshark

    Wireshark是一款非常流行的协议分析软件.自然可以网络抓包的需求. sudo apt-get install wireshark 出于安全方面的考虑,普通用户不能够打开网卡设备进行抓包,wire ...

  3. Qt实现串口通信总结

    Qt实现串口通信总结 注意: Qt5发布之前,Qt实现串口通信一般是采用第三方类库qextserialport.Qt5发布后自带了QtSerialPort 能够支持串口通信. 1.Qextserial ...

  4. (转)Vue2.X的路由管理记录之 钩子函数(切割流水线)

    $route可以在子组件任何地方调用,代表当前路由对象,这个属性是只读的,里面的属性是 immutable(不可变) 的,不过你可以 watch(监测变化)它. 导航和钩子函数: 导航:路由正在发生改 ...

  5. [Functional Programming ADT] Debug a Functional JavaScript composeK Flow

    When using ADTs in our code base, it can be difficult to use common debugging tools like watches and ...

  6. C中strstr的实现方法

    做题目的时候须要自己实现strstr函数 /************************************************************************/ /* 编 ...

  7. 并发编程系列小结(线程安全,synchronized,脏读,线程间的通信wait/notify,线程的三种实现方式Demo,可替代wait/notify的方法)

    线程安全: 当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法就是线程安全的) synchronized: 可以在任意对象或方法上加锁,而加锁的这段代码称为 ...

  8. kindeditor 图片上传插件

    富文本编辑器,kindeditor是比较好用的一款.需要的功能都有,文档.demo也详细.有什么功能去官网看一眼就好. 官网:http://kindeditor.net/ 一些好用的如图片上传,kin ...

  9. marquee.js

      图片无缝滚动工具类 CreateTime--2018年3月7日17:11:03 Author:Marydon /** * 图片无缝滚动 * @description * 将要滚动的图片复制一份作为 ...

  10. CSS3图片折角效果

    本篇文章由:http://xinpure.com/css3-picture-angle-effect/ 图片折角效果主要是通过设置 border 属性实现的效果 效果预览 效果解析 假设我们将一个元素 ...