POJ --- 2918 求解数独
Description
Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at our university and teaches newcomers like Jim. In the first lesson he explained the game of Tudoku to Jim. Tudoku is a straight-forward variant of Sudoku, because it consists of a board where almost all the numbers are already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of being too lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom left for him.
Sudoku is played on a 9 × 9 board that is divided into nine different 3 × 3 blocks. Initially, it contains only a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 × 3 block contains every number from 1 to 9. This can be quite hard but remember that Tom already filled most cells. A resulting Tudoku board can be solved using the following rule repeatedly: if some row, column or 3 × 3 block contains exactly eight numbers, fill in the remaining one.
In the following example, three cells are still missing. The upper left one cannot be determined directly because neither in its row, column, or block, there are eight numbers present. The missing number for the right cell can be determined using the above rule, however, because its column contains exactly eight numbers. Similarly, the number for the lower-most free cell can be determined by examining its row. Finally, the last free cell can be filled by either looking at its row, column or block.
7 | 5 | 3 | 2 | 8 | 4 | 6 | 9 | 1 |
4 | 8 | 2 | 9 | 1 | 6 | 5 | 3 | 7 |
1 | 9 | 6 | 7 | 5 | 3 | 8 | 4 | 2 |
9 | 3 | 1 | 6 | 4 | 2 | 5 | ||
2 | 7 | 5 | 4 | 9 | 1 | 3 | 8 | 6 |
6 | 4 | 8 | 3 | 2 | 1 | 7 | 9 | |
5 | 6 | 7 | 3 | 4 | 9 | 2 | 1 | 8 |
8 | 2 | 4 | 1 | 7 | 5 | 9 | 6 | 3 |
3 | 1 | 9 | 6 | 2 | 8 | 7 | 5 | 4 |
Input
The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digits each. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenario is terminated by an empty line.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for the input, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with a blank line.
Sample Input
2
000000000
817965430
652743190
175439820
308102950
294856370
581697240
903504610
746321580 781654392
962837154
543219786
439182675
158976423
627543918
316728549
895461237
274395861
Sample Output
Scenario #1:
439218765
817965432
652743198
175439826
368172954
294856371
581697243
923584617
746321589 Scenario #2:
781654392
962837154
543219786
439182675
158976423
627543918
316728549
895461237
274395861 思路:dfs,试填每个方格,当搜索的范围超过9×9时说明已经找到解。以前感觉挺难的,没敢写,今天下午写了一下感觉并不难,一次AC。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int map[][], flag;
bool CanPlace(int x, int y, int num){
for(int i = ; i <= ; i ++)
if(map[x][i] == num || map[i][y] == num) return false;
int row = ((x-)/)*+;
int col = ((y-)/)*+;
for(int i = row; i < row+; i ++){
for(int j = col;j < col+; j ++ )
if(map[i][j] == num) return false;
}
return true;
}
void dfs(int x, int y){
if(x == && y > ){
flag = ;
for(int i = ; i < ; i ++){
for(int j = ; j < ; j ++) printf("%d", map[i][j]);
printf("\n");
}
return;
}
if(y > ){
x++;
y = ;
}
if(!map[x][y]){
for(int i = ;i < ; i ++){
if(CanPlace(x, y, i)){
map[x][y] = i;
dfs(x, y+);
if(flag) return;
map[x][y] = ;
}
}
}else dfs(x, y+);
}
int main(){
char str[];
int t,cnt = ;
//freopen("in.c", "r", stdin);
scanf("%d", &t);
while(t--){
printf("Scenario #%d:\n", ++cnt);
memset(str, , sizeof(str));
for(int i = ; i < ; i ++){
scanf("%s", str);
for(int j = ; j < ; j ++){
map[i+][j+] = str[j]-'';
}
}
flag = ;
dfs(, );
puts("");
}
return ;
}
POJ --- 2918 求解数独的更多相关文章
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 求解数独难题, Sudoku问题(回溯)
Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...
- 算法实践——舞蹈链(Dancing Links)算法求解数独
在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dancing Links)算法求解精确覆盖问题. 本文介绍该算法的实际运用,利用舞蹈链(Dancin ...
- 关于用舞蹈链DLX算法求解数独的解析
欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 描述 在做DLX算法题中,经常会做到数独类型的题目,那么,如何求解数独类型的题目?其实,学了数独的构建方法,那么DL ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- 转载 - 算法实践——舞蹈链(Dancing Links)算法求解数独
出处:http://www.cnblogs.com/grenet/p/3163550.html 在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dan ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 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 ...
随机推荐
- 关于vs2013 mysql Ef框架中提示版本不兼容问题的解决办法
<runtime> <assemblyBinding> <dependentAssembly> <assemblyIden ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- C#并行和多线程编程_(1)认识Parallel
Parallel: 英 [ˈpærəlel] 美 [ˈpærəˌlɛl] ,并联的,并行的. 随着多核时代的到来,并行开发越来越展示出它的强大威力!使用并行程序,充分的利用系统资源,提高程序的性 ...
- ctf总结
在过去的一个学期中,草人在西普学院还有一些其他安全夺旗网站上刷了一些题,草人我是菜鸟一个,刚开始很是苦恼,所以经历过以后希望将之分享给一起学习安全的同学,目的呢是希望以后学习的人能尽快理清学习思路,进 ...
- Highcharts资料
对应的API: http://api.hcharts.cn/#chart.events 对应的中文网实例:http://www.hcharts.cn/demo/highcharts/dynamic ...
- 转 JavaScript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
收藏一下 1.判断select选项中 是否存在Value=”paraValue”的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.删除select中选中 ...
- MongoDB实战指南(二):索引与查询优化
数据库保存记录的机制是建立在文件系统上的,索引也是以文件的形式存储在磁盘上,在数据库中用到最多的索引结构就是B树.尽管索引在数据库领域是不可缺少的,但是对一个表建立过多的索引会带来一些问题,索引的建立 ...
- [杂题]CSUOJ1276 Counting Route Sequence
题目链接 题意:从1号点走到n号点(每条边只能走一次, 两结点间的边数必定为奇数) 问 经过结点不同顺序的方式有多少种(如1->2->3->4和1->3->2->4 ...
- Codeforces Round #238 (Div. 1)
感觉这场题目有种似曾相识感觉,C题还没看,日后补上.一定要坚持做下去. A Unusual Product 题意: 给定一个n*n的01矩阵,3种操作, 1 i 将第i行翻转 2 i 将第i列翻转 3 ...
- SQLite入门与分析(七)---浅谈SQLite的虚拟机
写在前面:虚拟机技术在现在是一个非常热的技术,它的历史也很悠久.最早的虚拟机可追溯到IBM的VM/370,到上个世纪90年代,在计算机程序设计语言领域又出现一件革命性的事情——Java语言的出现,它与 ...