Description

Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.

Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×2 pieces, every piece contains 1 to 4.

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!

Input

The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima.

It's guaranteed that there will be exactly one way to recover the board.

Output

For each test case, output one line containing Case #x:, where x is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.
Sample Input

****

*
*
*
*
**
***
*
**
Sample Output
Case #: Case #: Case #:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547

The 2015 China Collegiate Programming Contest

 ***********************************************

分析:一道比较简单的数独题目,这里简化了问题,变成了一个4*4的图,然后小图变成了2*2的图,这里问题就简洁了很多。

地图辣么小,直接枚举每一个点就行了。

从左上角开始,一直枚举到右下角,然后输出了就行了。

每次遇到一个“*”我们就枚举他变成1,2,3,4然后判断是否合法,如果合法就进行下一个点,否则回溯。

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 120000
#define INF 0x3f3f3f3f char a[][]; int judge(int x,int y)
{
int i,j; for(i=;i<;i++)///列判断,不能有相同的数字
if(a[x][i]==a[x][y]&&i!=y)
return ; for(i=;i<;i++)///行判断,不能有相同的数字
if(a[i][y]==a[x][y]&&i!=x)
return ; int row=x;
int col=y;
if(x%==)x-=;///找到小矩阵的左上角
if(y%==)y-=; for(i=x;i<=x+;i++)///小矩阵判断,不能有相同的数字
for(j=y;j<=y+;j++)
if(a[i][j]==a[row][col]&&i!=row&&j!=col)
return ; return ;
} void dfs(int x)///回溯dfs
{
int i,j; if(x==*)///如果遍历了所有点,就输出最终图形
{
for(i=;i<;i++)
{
for(j=;j<;j++)
printf("%c", a[i][j]);
printf("\n");
}
return ;
} int row=x/;///行的计算方法
int col=x%;///列的计算方法 if(a[row][col]=='*')
{
for(j=;j<=;j++)///枚举4个数字
{
a[row][col]=j+'';
if(judge(row,col))///如果当前这个点符合规则
dfs(x+);///进行下一步
a[row][col]='*';///记得要取消标记。(深搜尝试问题,涉及回溯)
}
}
else///如果不是,跳过,进行下一步
dfs(x+);
} int main()
{
int T,k=,i; scanf("%d", &T); while(T--)
{
for(i=;i<;i++)
scanf("%s", a[i]); printf("Case #%d:\n", k++); dfs();
}
return ;
}

HDU - 5547 Sudoku(数独搜索)的更多相关文章

  1. HDU 5547 Sudoku(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others ...

  2. HDU 1426 Sudoku Killer(搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...

  3. HDU 5547 Sudoku (暴力)

    题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...

  4. (hdu)5547 Sudoku (4*4方格的 数独 深搜)

    Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...

  5. E - Sudoku HDU - 5547 (搜索+暴力)

    题目链接:https://cn.vjudge.net/problem/HDU-5547 具体思路:对于每一位上,我们可以从1到4挨着去试, 具体判断这一位可不可以的时候,看当前这一位上的行和列有没有冲 ...

  6. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. HDU 1426 Sudoku Killer(dfs 解数独)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...

  8. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  9. HDU - 5547 数独(回溯法)

    题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 正所谓:骗分过样例,暴力出奇迹. 解题思想(暴力出奇迹(DFS+回溯)): 1 ...

随机推荐

  1. Visual Studio下使用jQuery的10个技巧

    广泛流行的jQuery是一个开源的,跨浏览器和兼容CSS 3的JavaScript库,你可以用它简化你的JavaScript编码任务和操作(添加,编辑和删除)HTML内容中的DOM元素,本文介绍10个 ...

  2. 疯狂java讲义--笔记

    第一章.Java语言概述与开发环境 什么是软件:一系列按照特定顺序组织的计算机数据和指令的集合: 交互方式:两种 GUI(Graphical User Interface) 图像界面 .CLI (Co ...

  3. LeetCode 395. Longest Substring with At Least K Repeating Characters C#

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. for i in xrange(0,5)使用过程中遇到的问题

    文件中共有4行内容. fd = open("C:\Users\william\Desktop\dup_file - Copy (2).txt")for i in xrange(0, ...

  5. 用sudo命令无法读取环境变量

    通过sudo -l来查看sudo的限制: $ sudo -l Matching Defaults entries for xxx on this host: env_reset, mail_badpa ...

  6. java回调机制(写的很好)

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273) 以前不理解什么叫回调,天天听人家说加一个回调方法啥的 ...

  7. mis 系统的开发具备的条件

    MIS的开发方式有自行开发.委托开发.联合开发.购买现成软件包进行二次开发几种形式.一般来说根据企业的技术力量.资源及外部环境而定. 补充: 管理信息系统的开发策略不可行的开发方法:组织结构法,机械的 ...

  8. java中多态的使用

    一.动手动脑 public class ParentChildTest { public static void main(String[] args) { Parent parent=new Par ...

  9. 安装eclipse要和JDK的位数相对应

    即JDK是32位时,eclipse也要装32位的

  10. python之~【空格】可不能随便加唷~

    上个礼拜学习从.proto文件转xxpb2.py文件的时候,明明成功了的. 结果周末的时候在家里,以及今天周一来到公司电脑,都遇到同样的一个问题. 我就纳闷了.这个路径确实存在呀. 而且我找遍了搜索引 ...