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. <四>面向对象分析之UML核心元素之用例

    一:基本概念        --->用例定义了一组用例实例,其中每个实例都是系统所执行一系列操作,这些操作生成特定主角可以观测的值.        --->所谓用例,就是一件事情,要完成这 ...

  2. 【转】No JVM could be found on your system解决方法

    原文网址:http://my.oschina.net/liusicong/blog/324964 在安装android studio时,报错: Error launching android Stud ...

  3. Redis Sentinel机制与用法

    概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都 ...

  4. redis错误汇总

    1.redis因为内存不够而启动失败 Microsoft Open Tech group 在 GitHub上开发了一个REDIS Win64的版本,项目地址是:https://github.com/M ...

  5. UVA 12532-Interval Product(BIT)

    题意: 给n个数字的序列,C v p 把v位上的数字置成p , S l r代表求区间[l,r]各数字相乘得数的符号,对于每个S输出所得符号(’+‘,’-‘,’0‘) 分析: 开两个数组表示区间负数的个 ...

  6. 使用Qmake在树莓派上开发Opencv程序

    Qt 安装 PC 端  下载安装即可 https://mirrors.ustc.edu.cn/qtproject/official_releases/qt 树莓派:Qt开发套件和opencv安装sud ...

  7. Chapter4:表达式

    左值和右值 当一个对象被用作右值的时候,用的是对象的值(内容),当对象被用作左值的时候,用的是对象的身份(在内存中的位置). 一个重要的原则是需要右值的地方可以用左值来代替,但是不能把右值当作左值使用 ...

  8. UML 学习

    推荐书籍:<面向对象分析与设计(第3版)>.<UML精粹:标准对象建模语言简明指南(第3版)> 推荐一: http://amateras.sourceforge.jp/cgi- ...

  9. Tengine新增健康检查模块

    总结 2.tengine的状态监控 Tengine的状态监控有两种 这里演示一个健康检查模块功能 配置一个status的location location /status { check_status ...

  10. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...