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 ...
随机推荐
- 重新安装Ubuntu12.04
重新安装Ubuntu12.04 之所以我重新安装Ubuntu,因为我第一次给根目录分配的空间过小,好像是20GB吧~结果编译Android的时候,编译了3个小时候直接中止掉了.郁闷.这个也告诉我们一定 ...
- 组织http请求
post方式 string stratTime=""; string end=""://要拼接的参数 string postURL = "http:/ ...
- 关于aspx模板页面元素路径的问题,以及对模板页面的理解
模板页面仅是模板,它不是单独存在的页面,它的路径就是引用它的内容页面的路径. 换句话说,模板页面,只是内容页面上固定的部分. 模板页面引用了的js和CSS,内容页面就不用重新引用了 css ...
- Bind Enum to ListControl
当使用MVVM时,相信你和我一样经常有这样的需求: 在ViewModel里定义了一个Enum,它必然是对应UI上的一个ListControl作为不同选项. 有一种做法是使用Converter,将Enu ...
- hdu 5429 Geometric Progression 高精度浮点数(java版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5429 题意:给一段长度不超过100的每个数字(可以是浮点数)的长度不超过1000的序列,问这个序列是否 ...
- Mac OS系统 - 将视频转换成gif
github中开源轻量级应用:droptogif
- DataGridView自动行号
最近又用了一下DataGridView控件,需要显示行号,我们知道在.net中DataGridView控件默认是不显示行号(数据的记录行数)的,后来通过查资料发现可以在DataGridView控件的R ...
- scrollview始终显示滚动条 Android
设置scrollview的:android:fadeScrollbars="false"表示始终显示垂直滚动条
- Linux中crond服务与crontab用法
需要写个在Linux下定时更新系统的脚本,man crondtab 不甚详细,现将网络上的介绍列举如下: crontab是一个很方便的在unix/linux系统上定时(循环)执行某个任务的程序使用cr ...
- Linux内核学习笔记: uid之ruid,euid,suid
转自: http://www.linuxidc.com/Linux/2011-09/43194.htm 看UNIX相关的书时经常能遇到这几个概念,但一直没有好好去理清这几个概念,以致对这几个概念一直一 ...