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. OpenLayers2中的事件_以Popup为例

    SATURDAY, 21 MARCH 1-Preface 前几天阅读学习了OpenLayers'Cookbook中的第四章——Working with events. 从AFDS系统的开发项目进行至今 ...

  2. Hive技术拾遗

    1. SELECT语句可以使用正则表达式做列选择,下面的语句查询除了ds和h 之外的所有列:SELECT `(ds|hr)?+.+` FROM sales 2. LEFT SEMI JOIN的限制是, ...

  3. nginx配置rewrite总结

    1.rewrite regex replacement [flag] 2.flag为break时,url重写后,直接使用当前资源,不在执行location里其他语句,完成本次请求,地址栏url不变. ...

  4. Android(Fragment和Activity之间通信)

    Fragment的使用可以让我们的应用更灵活的适配各种型号的安卓设备,但是对于Fragment和Activity之间的通信,很多朋友应该比较陌生,下面我们就通过一个实例来看一看如何实现. 一.Acti ...

  5. 深度增强学习--DQN的变形

    DQN的变形 double DQN prioritised replay dueling DQN

  6. Spark Streaming ReceiverTracker架构设计

    本节的主要内容: 一.ReceiverTracker的架构设计 二.消息循环系统 三.ReceiverTracker具体实现 Spark Streaming作为Spark Core基础 架构之上的一个 ...

  7. YARN Application执行流程

    原文见  http://xiguada.org/yarn-application_run/ 本节简单描述了一个Application在YARN上的执行流程,希望对初识YARN的同学提供一些帮助. 图1 ...

  8. 转: codereview 工具平台建设

    一.  Rietveld  code review  (一套工具系统与平台) 1. http://www.cnblogs.com/fang9159/archive/2012/07/20/2591690 ...

  9. JAVA的面向对象编程

    JAVA的面向对象编程 面向对象主要针对面向过程. 面向过程的基本单元是函数. 什么是对象:EVERYTHING IS OBJECT(万物皆对象) 全部的事物都有两个方面: 有什么(属性):用来描写叙 ...

  10. windows Visual Studio 2017 编译 HEVC cmake-3.8.1-win64-x64.msi 下载

    ttps://github.com/OpenHEVC/openHEVC 下载一 直接下载源码(可选)或下载源码包,我这里下载的是源码 打开 Visual Studio () 去 github 找到源码 ...