题目链接: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. Layui:设置select下拉框自动选中某项

    1.问题:layUI,在做编辑功能有下拉框数据时,需要初始化选中某个值,layUI官网没有相关api,可能我比较笨没找到 2.解决思路:出发点击事件 3.分析dom树结构,出发dl点击事件 方法: v ...

  2. 8 -- 深入使用Spring -- 2...5 Spring 3.0 新增的注解

    8.2.5 Spring 3.0 新增的注解 @DependsOn @Lazy @DependsOn :用于强制初始化其他Bean.修饰Bean类或方法,可以指定一个字符串数组作为参数,每个数组元素对 ...

  3. JS有趣的单线程

    一.JS的执行特点    源于单线程的特性, JS在一段时间内只能执行一部分代码, 那么, 当有多块代码需要执行时, 就需要排队等候了.   二.单线程与异步事件 (1) 什么是异步事件?     异 ...

  4. HTML 换行

    <br /> 标签可以用于换行 <!DOCTYPE HTML> <html> <body> <p> I like Playing. < ...

  5. TOMCAT可以稳定支持的最大并发用户数

    转自:http://blog.sina.com.cn/s/blog_68b7d2f50101ann7.html 服务器配置: 单硬盘,SATA   8MB缓存 测试服务器和loadrunner运行服务 ...

  6. 【译】Kafka最佳实践 / Kafka Best Practices

    本文来自于DataWorks Summit/Hadoop Summit上的<Apache Kafka最佳实践>分享,里面给出了很多关于Kafka的使用心得,非常值得一看,今推荐给大家. 硬 ...

  7. /etc/profile /etc/profile .bash_profile .bashrc解释

    1.用户登录系统时,bash首先执行/etc/profile配置文件和/etc/profile.d/目录下的配置文件,这些配置文件对系统的所有用户都有效,它们设置了普遍性的环境变量. 2.然后,Bas ...

  8. C#实现HTTP请求文件下载,GET、POST请求的数据流接收

    做项目的时候由于插件Phaser请求audio的时候,不允许跨域,具体提示====> 已拦截跨源请求:同源策略禁止读取位于 http://ttyouni.com/1.mp3 的远程资源.(原因: ...

  9. 原生js--事件类型

    1.表单事件: submit事件 reset事件 click事件 change事件 focus事件(不冒泡) (IE和ES5支持冒泡的focusin) blur事件(不冒泡) (IE和ES5支持冒泡的 ...

  10. 原生js(二)

    js的同步.异步和延迟 1.默认情况下,js是同步和阻塞DOM解析的.在解析DOM的过程中,当遇到script时,会暂停DOM解析,开始请求script并执行js,执行完成之后再接着解析DOM树. 2 ...