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 ...
随机推荐
- json,serialze之格式
<?php echo 'array-json:' . "\n"; $arr = array('key1'=>'value1', 'key2' => 'value2 ...
- 关于window.location不跳转的问题
今天在码代码的时候遇到个问题:html里采用onclick事件来实现window.location = url的跳转,在内嵌元素上又加上了href="javascrit:;"的属性 ...
- MySQL大数据量快速分页实现
一般刚开始学SQL语句的时候,会这样写 代码如下: SELECT * FROM table ORDER BY id LIMIT 1000, 10; 但在数据达到百万级的时候,这样写会慢死 代码如下: ...
- Linux下GPIO驱动(三) ----gpio_desc()的分析
上篇最后提出的疑问是结构体gpio_chip中的成员函数set等是怎么实现的,在回答之前先介绍下gpio_desc这个结构体. 如上图所示,右上方部分为GPIO驱动对其它驱动提供的GPIO操作接口,其 ...
- 针对目前高校移动App的火热,哥决定点一把火
最近正在做市场调研,还请众位大哥大姐们帮忙投个票,求扩散 http://user.qzone.qq.com/717010686/vote/00000000feb6bc2a3ebd1e53
- HttpContext
HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息.也有人叫上下文信息. 1.生存周期:从客户端用户点击并产生了一个向服务器发送请求开始---服务器处理完请求并生成返 ...
- 关于ASE日志空间示数不正常的解决办法
最近某系统的ASE数据库出现了异常,经过各种努力,终于把数据库正常又起起来了.但是经过检查,发现在查看剩余日志空间的时候(sp_helpsegment 'logsegment'),发现显示出来 ...
- protel DXP的类矢量图功能
一.概述 在写论文的过程中,我们经常需要将protel DXP上的原理图贴入到WORD中.我们可以选择使用截图工具,然后再导入到WORD中.但是由于普通截图图形文件都是位图文件,当我们将图形文件导入W ...
- CODEVS 2994 超级弹珠
题目描述 Description 奶牛们最近从著名的奶牛玩具制造商Tycow那里,买了一套仿真版彩蛋游戏设备.Bessie把她们玩游戏的草坪划成了N*N单位的矩阵,同时列出了她的K个对手在草地上的位置 ...
- [Linux发行版] 常见Linux系统下载
本专题页汇总最受欢迎的Linux发行版基本介绍和下载地址,如果您是一位刚接触Linux的新手,这里的介绍可能对您有所帮助,如果您是以为Linux使用前辈,也可以在评论处留下您宝贵意见和经验,以便让更多 ...