poj2676(数独)
也是一个简单剪枝的dfs。记录所有为0的位置,依次填写,当发现某个空格可选的填写数字已经没有时,说明该支路无效,剪掉。
不算是一个难题吧,但是还是花了不少时间,问题主要出在细节上,行列坐标反了、3乘3小格的位置判断等。写程序一定要细心。
#include <iostream>
using namespace std; const int MAX_R = ;
int map[MAX_R + ][MAX_R + ];
int zero_pos[MAX_R * MAX_R][];
int possible_digits[MAX_R * MAX_R][];
int zero_cnt; int setPossibleDigits(int x, int y, int s)
{
bool possible[];
memset(possible, , sizeof(possible));
for (int i = ; i <= MAX_R; i++){
possible[map[i][x]] = true;
possible[map[y][i]] = true;
}
int subX = (x - ) / ;
int subY = (y - ) / ;
for (int i = subX * + ; i <= subX * + ; i++){
for (int j = subY * + ; j <= subY * + ; j++){
possible[map[j][i]] = true;
}
}
int cnt = ;
for (int i = ; i <= ; i++){
if (!possible[i])
possible_digits[s][cnt++] = i;
}
return cnt;
} bool dfs(int step)
{
if (step == zero_cnt){
return true;
}
int curX = zero_pos[step][],
curY = zero_pos[step][];
int possible_cnt = setPossibleDigits(curX, curY, step);
if (possible_cnt == ){
return false;
}
for (int i = ; i < possible_cnt; i++){
map[curY][curX] = possible_digits[step][i];
if (dfs(step + ))
return true;
else
map[curY][curX] = ;
}
return false;
} int main()
{
int t;
cin >> t;
while (t--){
memset(map, , sizeof(map));
memset(zero_pos, , sizeof(zero_pos));
memset(possible_digits, , sizeof(possible_digits));
zero_cnt = ;
for (int i = ; i <= MAX_R; i++){
for (int j = ; j <= MAX_R; j++){
char ch;
cin >> ch;
map[i][j] = ch - '';
if (map[i][j] == ){
zero_pos[zero_cnt][] = i;
zero_pos[zero_cnt++][] = j;
}
}
}
dfs();
for (int i = ; i <= MAX_R; i++){
for (int j = ; j <= MAX_R; j++){
cout << map[i][j];
}
cout << endl;
}
}
return ;
}
poj2676(数独)的更多相关文章
- POJ2676 (数独问题 + DLX + 状态优化顺序)
(1)最简单的最是去暴力DFS搜索答案 , 很容易想到 , 每行每列的方式去搜索 , 不过效率是真的不行;但这个还是给出代码 ,毕竟打了也不容易呀! #include<cstdio> #i ...
- POJ2676,HDU4069解决数独的两种实现:DFS、DLX
搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...
- POJ2676 Sudoku [数独]
好题,也非常有用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programin ...
- POJ2676 – Sudoku(数独)—DFS
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24081 Accepted: 11242 Specia ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- LintCode389.判断数独是否合法
LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 数独 JAVA实现
数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...
随机推荐
- 用Emacs看电影
大多数人用emacs听歌,我却喜欢用emacs看电影.用 EMMS 和 mplayer 结合,看电影真是太方便了. 不要从源里安装EMMS,它可能给你安装别的播放器,没必要,我们有 mplayer 足 ...
- phpcm nginx 伪静态文件
rewrite ^/show-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid ...
- atan2 atan
atan2 definition In terms of the standard arctan function, whose range is (−π/2, π/2), it can be exp ...
- Go语言的接口interface、struct和组合、继承
Go语言的interface概念相对于C++中的基类,通过interface来实现多态功能. 在C++中,当需要实现多态功能时,步骤是首先定义一个基类,该基类使用虚函数或者纯虚函数抽象了所有子类会用到 ...
- 最短路径—Floyd算法
Floyd算法 所有顶点对之间的最短路径问题是:对于给定的有向网络G=(V,E),要对G中任意两个顶点v,w(v不等于w),找出v到w的最短路径.当然我们可以n次执行DIJKSTRA算法,用FLOYD ...
- a标签、img图片、iframe、表单元素、div
1.<a href="http://www.baidu.com" target=''_blank">百度</a> 超链接标签 2.<img ...
- Ubuntu下使用virtualenv
Ubuntu 18.04,Python 3.6.5(最新3.7),virtualenv 16.0.0, 即将在Ubuntu上大张旗鼓地干活啦!那么,将之前安装的virtualenv运行起来吧(前面都是 ...
- 【web开发】web前端开发常用技术总结归纳
技术选型规范规范 • Vue版本:2.x • 前端路由:vue-route • 异步请求:Axios • 全局状态管理:VueX • css预处理器:sass/less • h5项目移动端适配规则:使 ...
- python并行计算(持续更新)
工作中需要对tensorflow 的一个predict结果加速,利用python中的线程池 def getPPLs(tester,datas): for line in datas: tester(l ...
- Inno setup 常用修改技巧
Inno setup 常用修改技巧1 .如何让协议许可页面默认选中我同意按钮 [code]procedure InitializeWizard();beginWizardForm.LICENSEACC ...