Tudoku
 

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 求解数独的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 求解数独难题, Sudoku问题(回溯)

    Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...

  4. 算法实践——舞蹈链(Dancing Links)算法求解数独

    在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dancing Links)算法求解精确覆盖问题. 本文介绍该算法的实际运用,利用舞蹈链(Dancin ...

  5. 关于用舞蹈链DLX算法求解数独的解析

    欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 描述 在做DLX算法题中,经常会做到数独类型的题目,那么,如何求解数独类型的题目?其实,学了数独的构建方法,那么DL ...

  6. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  7. 转载 - 算法实践——舞蹈链(Dancing Links)算法求解数独

    出处:http://www.cnblogs.com/grenet/p/3163550.html 在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dan ...

  8. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  9. 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 ...

随机推荐

  1. 关于.NET技术前途问题的讨论

    我去年曾经在论坛发起过关于.NET技术前途问题这个话题的讨论,也引起了很多同行和朋友的回复,时间过去大半年,自己也有了一些新的理解.本文的目的就是将其中一些精彩的观点整理出来并谈谈自己的观点. 引子 ...

  2. 《jQuery、jQuery UI及jQuery Mobile技巧与示例》勘误收集

    此书由程学彬 (http://weibo.com/ironbin)和我合译完成,此篇博客作为勘误收集而用,若译文有误或者有任何疑问,欢迎留下评论,或者给我发邮件(地址:gzooler@gmail.co ...

  3. HTML5的简介

    前言:作为IOS开发工程师,终会接触到网页前端开发,甚至可能会有 用HTML5开发IOS的app客户端的需求.比如现在上架的app就有比如理财类型的app有的就用HTML开发的,从理财类型的app需求 ...

  4. 如何让VS2012同时调试2个项目

  5. ASP.NET 学习小记 -- “迷你”MVC实现(2)

    Controller的激活 ASP.NET MVC的URL路由系统通过注册的路由表对HTTO请求进行解析从而得到一个用户封装路由数据的RouteData对象,而这个过程是通过自定义的UrlRoutin ...

  6. Unity3d Shader开发(二)SubShader

    (1)SubShader Unity中的每一个着色器都包含一个subshader的列表,当Unity需要显示一个网格时,它能发现使用的着色器,并提取第一个能运行在当前用户的显示卡上的子着色器. 当Un ...

  7. bootstrap form

    http://getbootstrap.com/examples/starter-template/ <form class="form-horizontal" role=& ...

  8. SQL语句中使用条件逻辑

    select name, sal, case when sal >= 4000 then 'Good' when sal <= 2000 then 'Bad' else 'Ok' end ...

  9. C#.net调用axis2webService

    用C#.net调用axis2webService的时候需要引用web服务, 比如访问地址为:http://111.21.32.213:8080/axis2/services/AdService/get ...

  10. js实现方法的链式调用

    假如这里有三个方法:person.unmerried();person.process();person.married();在jQuery中通常的写法是:person.unmerried().pro ...