POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676
Time Limit: 2000MS Memory Limit: 65536K
Description

Input
Output
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]的更多相关文章
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- 蓝桥杯---数独(模拟 || dfs)
[编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...
- 蓝桥杯 带分数 DFS应用
问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- 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 ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
随机推荐
- 10 -- 深入使用Spring -- 5...1 使用Quartz
10.5.1 使用Quartz JDK为简单的任务调度提供了Timer支持. Quartz是一个任务调度框架.借助于Cron表达式,Quartz可以支持各种复杂的任务调度. 1.下载和安装Quartz ...
- zabbix添加对tomcat线程池的监控
在zabbix模板中添加以下监控项: 可以参考文档:http://www.fblinux.com/?p=616
- iOS开发-修改UITableViewCell中image和title的位置和大小
最近在开发中遇到需要Cell中imageView和textLable位置和大小的情况,设计希望得到的结果如下图所示: 而TableViewCell默认样式,image是靠紧左边的,并且image和ti ...
- nmap 中的idle scan
http://www.offensive-security.com/metasploit-unleashed/Port_Scanning http://blog.csdn.net/dong976209 ...
- vue再次入手(数据传递①)
准备 之前使用vue.js完成一个项目之后,对其还是充满着无限兴趣,于是不妨利用碎片时间再次研究一下这个“令人着迷”的js框架. 1.新建一个基于vue的项目,具体方法不再赘述,请看这里:http:/ ...
- django初体验 学习笔记
django环境搭建 1.安装Python 2.ipython sudo apt-get install ipython sudo pip instal ...
- PHP 连接oracle
function connect_oracle(){ static $dbconn = false; if(!$dbconn){ $db_server = "127.0.0.1"; ...
- iptables常用规则
删除现有规则 iptables -F (OR) iptables --flush 设置默认链策略 iptables的filter表中有三种链:INPUT, FORWARD和OUTPUT.默认的链策略是 ...
- open-falcon之query
功能 query组件,提供统一的绘图数据查询入口.query组件接收查询请求,根据一致性哈希算法去相应的graph实例查询不同metric的数据,然后汇总拿到的数据,最后统一返回给用户. 配置文件 { ...
- java框架---->commonmark的使用(一)
commonmark-java是一个Markdown 解析器,一个基于CommonMark规范解析和渲染Markdown文本的Java库.偶尔要回头看看,否则永远都在追寻,而不知道自己失去了什么. c ...