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. spring mvc 实现文件上传下载

    /** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...

  2. 【转】CSS

    css概念 http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html css八大功能 http://developer.51ct ...

  3. 取消a标签在移动端点击时的背景颜色

    一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0); -webkit-user-select: none; -m ...

  4. Starting httpd: (98)Address already in use: make_sock: could not bind to address [::]:80

    netstat -tulpn| grep :80 killall -9 httpd /etc/init.d/httpd start  or service httpd start

  5. ListView 分页 排序、编辑、插入和删除

    摘自网络地址:http://msdn.microsoft.com/zh-cn/magazine/cc337984.aspx ListView 基础 ListView 是模板驱动的控件,这意味着它默认情 ...

  6. AtomicInteger的用法

    J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...

  7. 如何阅读一本书([美] 莫提默·J. 艾德勒 / 查尔斯·范多伦 )

               进入豆瓣读书 前言 2017年1月2日跟着熊猫书院开始了为期十月的阅读计划. 熊猫书院是一个微信公众号,但仅对熊猫书院学员开放.它是一个很好的读书产品,从入学申请.入学报到.班长 ...

  8. 一个例子让你了解Java反射机制

    本文来自:blog.csdn.net/ljphhj JAVA反射机制: 通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运 ...

  9. Ubuntu下安装composer及配置

    1.下载最新composer wget -c https://getcomposer.org/composer.phar 2.可执行权限 chmod u+x composer.phar 3.放置到安装 ...

  10. linux截取字符串的多种方法

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.hao.com/123.htm . . 一 # 号截取,删除左边字符,保留右边字符. echo ${var#*/ ...