Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14368   Accepted: 7102   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

Source

 
http://bailian.openjudge.cn/practice/2982/
 
题目大意:就是数独咯,让你求解数独游戏,9乘9的矩阵要求每行每列和9个3*3的子矩阵内都出现数字1-9
 
题目分析:9乘9的矩阵,从第一个位置一直搜到最后一个位置,若当前位置有数字搜下一位置,否则枚举1-9并判断,
判断时当前行r = n/9当前列为c = n%9当前子矩阵的第一个元素位置为r / 3 * 3,c / 3 * 3
 #include <cstdio>
char s[];
int num[][];
bool flag; bool ok(int n, int cur)
{
int r = n / ; //当前行
int c = n % ; //当前列
for(int j = ; j < ; j++) //枚举列
if (num[r][j] == cur)
return false;
for(int i = ; i < ; i++) //枚举行
if (num[i][c] == cur)
return false;
//得到当前所在的子矩阵的第一个元素位置
int x = r / * ;
int y = c / * ;
//枚举子矩阵中的元素
for(int i = x; i < x + ; i++)
for(int j = y; j < y + ; j++)
if (num[i][j] == cur)
return false;
return true;
} void DFS(int n)
{
if(n > || flag)
{
flag = true;
return;
}
if(num[n / ][n % ])//当前位置有数字直接搜索下一位
{
DFS(n + );
if(flag)
return;
}
else
{
for(int cur = ; cur <= ; cur++) //枚举数字
{
if(ok(n, cur)) //若ok则插入
{
num[n / ][n % ] = cur;
DFS(n + );
if(flag)
return;
num[n / ][n % ] = ; //还原
}
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
flag = false;
for(int i = ; i < ; i++) //得到数独矩阵
{
scanf("%s", s);
for(int j = ; j < ; j++)
num[i][j] = (s[j] - '');
}
DFS(); //从第一位开始搜
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
printf("%d", num[i][j]);
printf("\n");
}
}
}

题解来源:http://blog.csdn.net/tc_to_top/article/details/43699047

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

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

  2. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

  3. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  4. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

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

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

  6. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  7. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  8. DFS POJ 2676 Sudoku

    题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...

  9. POJ 2676/2918 数独(dfs)

    思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...

随机推荐

  1. [转]PHP: 深入pack/unpack

    From : http://my.oschina.net/goal/blog/195749 http://www.w3school.com.cn/php/func_misc_pack.asp PHP作 ...

  2. ThinkPHP错误信息处理

    index.php入口文件中打开APP_DEBUG// 开启调试模式define('APP_DEBUG', TRUE); // 开启Trace信息 'SHOW_PAGE_TRACE' =>tru ...

  3. CPLUSPLUS 获得 一个源文件的头文件依赖。即该文件所需要的所有头文件

    核心命令:gcc -M *.h.*.cpp 转: 自动处理头文件的依赖关系 http://blog.csdn.net/su_ocean16/article/details/5374696 现在我们的M ...

  4. maven-assembly-plugin 入门指南

    当你使用 Maven 对项目打包时,你需要了解以下 3 个打包 plugin,它们分别是 plugin function maven-jar-plugin maven 默认打包插件,用来创建 proj ...

  5. Python traceback 模块, 打印异常信息

    Python感觉是模仿Java, 到处都需要加try..catch.... 这里记录一下用法,方便后续使用. # -*- coding:utf-8 -*- import os import loggi ...

  6. Mac-OSX下Ruby更新

    Mac下是自带Ruby环境的,在有些情况我们是需要更新Ruby的,安装和更新Ruby环境可以通过rvm命令进行操作,rvm在安装过程中通过HomeBrew安装依赖包,如果之前没有装过HomeBrew, ...

  7. Gradle Groovy 基础语法 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Trie树分词

    http://www.hankcs.com/program/java/tire-tree-participle.html 最近在看Ansj中文分词的源码,以前没有涉足过这个领域,所以需要做一些笔记. ...

  9. (转)Unity3D研究院之Assetbundle的实战(六十三)

    上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一) 本篇文章我们将说说assetbundl ...

  10. Node.js中 express-session的奇怪问题

      var session = require('express-session');   app.use(cookieParser()); 行 登录以后访问的时候有时候会报这样的错误: The la ...