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,看了下那个一些有关于 ...
随机推荐
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )
~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...
- 在Django中Session的那点事!
1.session是什么 首先引入度娘的解释:Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 We ...
- Android选择头像
http://www.jianshu.com/p/8b3e78046c1c 注意:在Android6.0之后,使用相机拍照需要权限 在选择头像使用相机拍摄时添加以下代码即可. Acp.getInsta ...
- Java学习笔记——继承、接口、多态
浮点数的运算需要注意的问题: BigDecimal operand1 = new BigDecimal("1.0"); BigDecimal operand2 = new BigD ...
- 【codeforces】940F题解
CF Round #466的最后一题,颇有难度,正解是带修改莫队算法. [题意] 给定一个长度为\(n\)的数组\(a\),并且要求执行\(q\)个操作,有两种不同的操作: ①询问一个区间\([l,r ...
- SVC 工作过程中出现的错误记录(SEO项目)
1.同一のキーを含む項目が既に追加されています.追加的项目中含有重复主键) /seo' アプリケーションでサーバー エラーが発生しました. 同一のキーを含む項目が既に追加されています. 説明: 現在の ...
- C基础 - 终结 Size Balanced Tree
引言 - 初识 Size Balanced Tree 最近在抽细碎的时间看和学习 random 的 randnet 小型网络库. iamrandom/randnet - https://github. ...
- 04 Go 1.4 Release Notes
Go 1.4 Release Notes Introduction to Go 1.4 Changes to the language For-range loops Method calls on ...
- 在VS中让一个JS文件智能提示另一个JS文件中的成员2--具体引用
我们知道,在html中,利用<script language="javascript" type="text/javascript" src=" ...