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 ...
随机推荐
- 支持HTML5新标签
IE8/IE7/IE6支持通过document.createElement方法产生的标签, 可以利用这一特性让这些浏览器支持HTML5新标签, ...
- C#基础(七)——静态类与非静态类、静态成员的区别
静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来实例 ...
- 在Windows Server 2008上部署SVN代码管理总结
这段时间在公司开发Flex程序,所以使用TortoiseSVN作为团队代码管理器,今天在公司服务器上部署SVN服务器,并实验成功,总结如下: 服务器环境: 操作系统:Windows Server 20 ...
- PL/SQL — 存储过程
存储过程子程序的一种类型,能够完成一些任务,作为schema对象存储于数据库.是一个有名字的PL/SQL代码块,支持接收或不接受参数,同时也支持参数输出.一个存储过程通常包含定于部分,执行部分,Exc ...
- 成为Java GC专家(3)—如何优化Java垃圾回收机制
为什么需要优化GC 或者说的更确切一些,对于基于Java的服务,是否有必要优化GC?应该说,对于所有的基于Java的服务,并不总是需要进行GC优化,但前提是所运行的基于Java的系统,包含了如下参数或 ...
- leetcode-110:判断平衡二叉树 Java
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- Js通过原型继承创建子类
//定义一个有两个方法的类 function Person(){} Person.prototype.married = function(){}; Person.prototype.unmerrie ...
- Bengio最新博文:深度学习展望
Bengio最新博文:深度学习展望 人类一直梦想着创造有智能的机器.早在第一台可编程计算机问世前100多年,发明家就对如何能让由连杆和齿轮组成的设备也变得更加智能这一命题充满好奇.后来,20世纪40年 ...
- iOS 并发:NSOperation 与调度队列入门(1)
一直以来,并发都被视为 iOS 开发中的「洪水猛兽」.许多开发者都将其视为危险地带,唯恐避之而不及.更有谣传认为,多线程代码应该尽力避免.笔者同意,如果你对并发的了解不够深入,就容易造成危险.但是,危 ...
- 1920-Jangbi的Rush
描述 最后一届的OSL决赛由神族的Jangbi对阵人族Fantasy.Jangbi5BG爆叉叉准备一波rush,但是范特西早有防备,在地图上埋下了许多地雷.但是Jangbi显然不是毕姥爷那样的无脑平A ...