题目链接:http://poj.org/problem?id=2676

Time Limit: 2000MS Memory Limit: 65536K

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

题意:

你一定听说过“数独”游戏。
如【图1.png】,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。

数独的答案都是唯一的,所以,多个解也称为无解。

本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。

本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。

格式要求:
输入9行,每行9个数字,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。

题解:

从[1,1]到[9,9]地进行DFS

AC代码:

#include<cstdio>
#include<cstring>
using namespace std; int num[][];
bool rowVis[][],colVis[][],matVis[][]; struct Pos{
int row,col;
Pos nextpos()
{
if(col==) return (Pos){row+,};
else return (Pos){row,col+};
}
int MatID(){return (row-)/* + (col-)/+;}
bool ok(int x)
{
if(!rowVis[row][x] && !colVis[col][x] && !matVis[MatID()][x]) return ;
else return ;
}
}; void mark(Pos pos,int x,bool val)
{
rowVis[pos.row][x] = val;
colVis[pos.col][x] = val;
matVis[pos.MatID()][x] = val;
} bool dfs(Pos pos)
{
int row=pos.row,col=pos.col; if(row==) return ;
if(num[row][col]) return dfs(pos.nextpos()); for(int i=;i<=;i++)
{
if(pos.ok(i))
{
num[row][col]=i; mark(pos,i,);
if(dfs(pos.nextpos())) return ;
num[row][col]=; mark(pos,i,);
}
}
return ;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(rowVis,,sizeof(rowVis));
memset(colVis,,sizeof(colVis));
memset(matVis,,sizeof(matVis));
for(int row=;row<=;row++)
{
char tmp[]; scanf("%s",tmp);
for(int col=;col<=;col++)
{
num[row][col]=tmp[col-]-'';
if(num[row][col]) mark((Pos){row,col},num[row][col],);
}
} dfs((Pos){,}); for(int row=;row<=;row++)
{
for(int col=;col<=;col++) printf("%d",num[row][col]);
printf("\n");
}
}
}

POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

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

  2. ACM : POJ 2676 SudoKu DFS - 数独

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

  3. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...

  4. 蓝桥杯 带分数 DFS应用

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...

  5. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

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

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

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

  8. poj 2676 Sudoku ( dfs )

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

  9. POJ 2676 Sudoku (DFS)

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

随机推荐

  1. 微信公众号支付-Common

    using System.Web; /// <summary> /// 公共帮助类 /// </summary> public class Common { private H ...

  2. SpringBoot------热部署(Springloaded)

    为啥要热部署: 在修改代码的时候,不需要重新启动程序,程序会自动进行编译 注意: 控制器中新增加的方法是不能进行热部署的 方法: 1.在pom.xml文件里面添加下面代码 <project> ...

  3. AngularJS------使用VSCode创建的Angular项目部署IIS

    转载: http://www.cnblogs.com/kingkangstudy/p/7699710.html 1.进入项目src,执行命令行:ng build 2.步骤1后会生成dist文件 3.打 ...

  4. 小物件之checkbox复选框

    有时候需要输出一组checkbox复选框,并且做根据选定元素将其选中的功能,以往都要在模板中循环输出checkbox标签,同时加以判断是否需要选中,这样就会造成很多开始闭合标签 以前都是这样写 现在我 ...

  5. C++ mysql 乱码

    C++读mysql数据库中的中文显示出来的是乱码 在连接到数据库后加上这么一句 mysql_query(pMYSQL, "SET NAMES GB2312"); 或者 mysql_ ...

  6. Libjingle库 综述

    国内现在很多语音聊天工具都是基于TURN方式实现的,包括YY.AK等等,这种方式对于服务器的性能要求很高,而且在用户量增大的时候,服务器压力也会越来越大,用户的语音质量也会受到很大影响.而基于P2P方 ...

  7. Linux 查看目录大小及文件数量命令

    查看当前目录大小: [root@21andy.com]# du -sh 查看指定目录大小: [root@21andy.com]# du -sh /www/21andy.com 查看当前目录文件总数: ...

  8. 为啥RESTFULL如此重要?

    为啥RESTFULL如此重要? 2014-6-3 20:13| 发布者: admin| 查看: 57| 评论: 0|来自: java365 摘要: 本文我们将讨论REST,它定义了一组体系架构原则,您 ...

  9. iOS 静态库和动态库(库详解)

    什么是库 ? 库就是程序代码的集合,将N个文件组织起来,是共享程序代码的一种方式.库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行. 库的分类 开源库:源代码是公开的,可以看到每个实现 ...

  10. Android学习之BitMap用法实例

    下面简单说明了BitMap的用法: 从服务器下载一张图片,显示在ImageView控件上,并将该图片保存在移动设备的SD上. // 根据网络URL获取输入流 public InputStream ge ...