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,看了下那个一些有关于 ...
随机推荐
- linux nginx服务 反向代理 负载均衡 nfs服务
一.nginx服务 1.首先软件停用firewall #systemctl stop firewalld stop:本次停用 disable:开机停用 enable:开机启用 #ps aux | gr ...
- python初步学习-python 模块之 sys(持续补充)
sys sys 模块包括了一组非常实用的服务,内含很多函数方法和变量 sys 模块重要函数变量 sys.stdin 标准输出流 sys.stdout 标准输出流 sys.stderr 标准错误流 sy ...
- PL/SQ连接oracle,L 新建表的时候, virtual那一列是什么意思
Virtual标示该栏位是否为虚拟列. https://www.2cto.com/database/201306/216917.html
- AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;大坑
这个问题太坑了,试了好多个版本,都是依赖冲突导致的, https://blog.csdn.net/qq_15003505/article/details/78430595 最后找到这一篇博客解决了,就 ...
- imperva-指定url禁止访问
指定url禁止访问 应用到那个网站 访问一下查看告警
- gcc -O0 -O1 -O2 -O3 四级优化选项及每级分别做什么优化【转】
转自:http://blog.csdn.net/qinrenzhi/article/details/78334677 相关博客http://blog.chinaunix.net/uid-2495495 ...
- 2015 Dhaka
2015 Dhaka A - Automatic Cheater Detection solution 模拟计数. B - Counting Weekend Days solution 模拟计数. C ...
- file.getPath() getAbsolutePath() getCanonicalPath()区别
package file; import java.io.File; import java.io.IOException; public class getFilePath { public sta ...
- 使用SQL语句查询某表中所有的主键、唯一索引以及这些主键、索引所包含的字段(转)
SELECT 索引名称 = a.name , 表名 = c.name , 索引字段名 = d.name , 索引字段位置 = d.colid FROM sysindexes a JOIN sysind ...
- python中mock的使用
什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...