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. sqlserver 中WITH NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX

    https://www.cnblogs.com/sthinker/p/5922967.html SqlServer查询语句中用到的锁 作者: wokofo 前段时间**公司DBA来我们这培训.讲了一大 ...

  2. LCA算法总结

    LCA问题(Least Common Ancestors,最近公共祖先问题),是指给定一棵有根树T,给出若干个查询LCA(u, v)(通常查询数量较大),每次求树T中两个顶点u和v的最近公共祖先,即找 ...

  3. 京东的Netty实践,京麦TCP网关长连接容器架构

    背景 早期京麦搭建 HTTP 和 TCP 长连接功能主要用于消息通知的推送,并未应用于 API 网关.随着逐步对 NIO 的深入学习和对 Netty 框架的了解,以及对系统通信稳定能力越来越高的要求, ...

  4. iOS:UICollectionView的扩展应用

    一.介绍 CollectionView是iOS中一个非常重要的控件,它可以实现很多的炫酷的效果,例如轮播图.瀑布流.相册浏览等.其实它和TableView很相似,都是对cell进行复用,提高系统性能. ...

  5. verilog语法实例学习(3)

    Verilog 操作运算符 算术运算符 +,-,*,/,**(加/减/乘/除/幂运算),乘法运算的结果的位宽是乘数和被乘数位宽的和. 在进行整数的除法运算时,结果要略去小数部分,只取整数部分:而进行取 ...

  6. 【转】Redis 总结精讲 看一篇成高手系统-4

    https://www.cnblogs.com/rjzheng/p/9096228.html 本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么 ...

  7. 非常好的课程,尤其是有NLP方向的内容,好好学习

    课程地址如下: https://mooc.study.163.com/smartSpec/detail/1001319001.htm 有一个非常好的笔记: https://github.com/fen ...

  8. SCRIPT5009: “Sys”未定义 部署.net ajax 解决方案

    今天在部署asp.net ajax 的时候发现部署服务器的时候,ajax刷新不正确,开始以为是System.Web.Extensions没有引用到本地,baidu一圈发现(最近google上不了郁闷~ ...

  9. vue2.0路由-适合刚接触新手简单理解

    vue路由:vue-router vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并 ...

  10. Hibernate缓存应用的积累与总结

    Hibernate缓存一直比较难掌握,下面就分析和总结原因,相信你就会慢慢清楚了原来Hibernate缓存也是可以轻松掌握的,但前提要求大家必须跟着动手去验证一下,再用心体会,光看是没有用的 目录: ...