H - Sudoku

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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

3

****
2341
4123
3214 *243
*312
*421
*134 *41*
**3*
2*41
4*2*

Sample Output

Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123

HINT

题意

让你找到一个4*4的数独的合法解

题解:

直接爆搜就能过

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std; string s[];
int p[][];
int tx[];
int ty[];
int tot = ;
int flag;
int vis[];
int check()
{
for(int i=;i<;i++)
{
vis[]=vis[]=vis[]=vis[]=;
for(int j=;j<;j++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
}
for(int j=;j<;j++)
{
vis[]=vis[]=vis[]=vis[]=;
for(int i=;i<;i++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
}
vis[]=vis[]=vis[]=vis[]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
} vis[]=vis[]=vis[]=vis[]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
}
vis[]=vis[]=vis[]=vis[]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
}
vis[]=vis[]=vis[]=vis[]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(p[i][j]==)continue;
if(vis[p[i][j]])return ;
vis[p[i][j]]=;
}
}
return ;
}
void dfs(int x)
{
if(flag)return;
if(x==tot){
for(int i=;i<;i++)
{
for(int j=;j<;j++)
printf("%d",p[i][j]);
printf("\n");
}
flag=;
return;}
for(int i=;i<=;i++)
{
p[tx[x]][ty[x]]=i;
if(check())
dfs(x+);
p[tx[x]][ty[x]]=;
}
}
int main()
{
int t;scanf("%d",&t);
for(int cas = ;cas <= t;cas++)
{
tot = ;
flag = ;
for(int i=;i<;i++)
cin>>s[i];
for(int i=;i<;i++)
for(int j=;j<;j++)
if(s[i][j]=='*')
p[i][j]=;
else
p[i][j]=s[i][j]-''; for(int i=;i<;i++)
for(int j=;j<;j++)
if(p[i][j]==)
{
tx[tot]=i;
ty[tot]=j;
tot++;
}
printf("Case #%d:\n",cas);
dfs();
}
}

2015南阳CCPC H - Sudoku 暴力的更多相关文章

  1. 2015南阳CCPC H - Sudoku 数独

    H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g ...

  2. 2015南阳CCPC G - Ancient Go 暴力

    G - Ancient Go Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yu Zhou likes to play Go wi ...

  3. 2015南阳CCPC D - Pick The Sticks dp

    D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened lon ...

  4. 2015南阳CCPC A - Secrete Master Plan 水题

    D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Master Mind KongMing gave ...

  5. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

  6. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  7. 2015南阳CCPC L - Huatuo's Medicine 水题

    L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous ...

  8. 2015南阳CCPC G - Ancient Go dfs

    G - Ancient Go Description Yu Zhou likes to play Go with Su Lu. From the historical research, we fou ...

  9. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

随机推荐

  1. HDU 5114 Collision

    Collision Time Limit: 15000/15000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total ...

  2. storm入门教程 第一章 前言[转]

    1.1   实时流计算 互联网从诞生的第一时间起,对世界的最大的改变就是让信息能够实时交互,从而大大加速了各个环节的效率.正因为大家对信息实时响应.实时交互的需求,软件行业除了个人操作系统之外,数据库 ...

  3. loadrunner 发送gzip压缩json格式(转)

    转:http://blog.csdn.net/gzh0222/article/details/7711281 使用java vuser实现,发送gzip压缩json格式. /* * LoadRunne ...

  4. ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别

    Response.Write 与   Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...

  5. G-sensor驱动分析

    重力传感器代码分析 重力传感器驱动的功能,主要是向HAL层提供IOCTRL接口,并通过input设备上报数据.芯片实际数据的读取是采用i2c协议读取原始数据,并且作为i2c设备挂载在系统上工作的. 1 ...

  6. mybatis系列-12-多对多查询

    12.1     需求 查询用户及用户购买商品信息. 12.2     sql语句 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关联表: orders.or ...

  7. 虚拟化技术对比:Xen vs KVM

    恒天云:http://www.hengtianyun.com/download-show-id-68.html 一.说明 本文主要从功能方面和性能方面对Xen和KVM对比分析,分析出其优缺点指导我们恒 ...

  8. HBase Shell手动移动Region

    在生产环境中很有可能有那么几个Region比较大,但是都运行在同一个Regionserver中. 这个时候就需要手动将region移动到负载低的Regionserver中. 步骤: 1.找到要移动的r ...

  9. oracle 10g

    一.安装系统 首先安装Linux系统,根据Oracle官方文档的建议,在机器内存小于1G的情况下,swap分区大小应该设置为内存的2倍大,若内存大于2G则swap分区设置为与内存大小一样. 为防止Or ...

  10. 从根源上解析 Java volatile 关键字的实现

    1.解析概览 内存模型的相关概念 并发编程中的三个概念 Java内存模型 深入剖析Volatile关键字 使用volatile关键字的场景 2.内存模型的相关概念 缓存一致性问题.通常称这种被多个线程 ...