2015南阳CCPC H - Sudoku 数独

体验古时候智谋游戏的魅力,通过还原被隐藏的数独板,为历史人物 YiSima 带来欢乐并晋升。 游戏规则:在4x4的数独板上,每行和每列都包含1到4的数字,且四个2x2的区域也各自包含1到4的数字。部分数字已被隐藏,玩家需通过逻辑推理,还原数独板,满足所有规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

H - Sudoku

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


题意:给你4*4的图,数独游戏,4个2*2的块独立,每行每列独立
题解:暴力

///1085422276
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a))
#define TS printf("111111\n")
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************
#define maxn 5
bool flag;
int ans=0;
char mp[maxn][maxn];
bool test()
{
    if((mp[0][0]-'0')*(mp[0][1]-'0')*(mp[1][0]-'0')*(mp[1][1]-'0')!=24)return 0;
    //if((mp[0][2]+mp[0][3]+mp[1][2]+mp[1][3]-'0'-'0'-'0'-'0')!=10)return 0;
     if((mp[0][2]-'0')*(mp[0][3]-'0')*(mp[1][2]-'0')*(mp[1][3]-'0')!=24)return 0;
  //  if((mp[2][0]+mp[2][1]+mp[3][0]+mp[3][1]-'0'-'0'-'0'-'0')!=10)return 0;

     if((mp[2][0]-'0')*(mp[2][1]-'0')*(mp[3][0]-'0')*(mp[3][1]-'0')!=24)return 0;
  //  if((mp[2][2]+mp[2][3]+mp[3][2]+mp[3][3]-'0'-'0'-'0'-'0')!=10)return 0;

     if((mp[2][2]-'0')*(mp[2][3]-'0')*(mp[3][2]-'0')*(mp[3][3]-'0')!=24)return 0;
    return 1;
}
void dfs(int x,int y,int t){
    if(t==ans){
          //  cout<<1<<endl;
        if(test()){
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    printf("%c",mp[i][j]);
                }
                cout<<endl;
                 flag=1;
            }
        }
        return ;
    }

    if(flag)return ;

            if(mp[x][y] == '*'){
                for(int k=1;k<=4;k++){
                    int flags=0;
                    for(int i=0;i<4;i++){if(y!=i&&mp[x][i]==k+'0')flags=1;}
                      for(int i=0;i<4;i++){if(x!=i&&mp[i][y]==k+'0')flags=1;}
                    if(flags)continue;

                    mp[x][y]=k+'0';
                    if(y==3){dfs(x+1,0,t+1);}
                    else {dfs(x,y+1,t+1);}
                      if(flag)return ;
                    mp[x][y]='*';
                }
            }
            else {
                    if(y==3)
                dfs(x+1,0,t);
                else dfs(x,y+1,t);
            }

}
int main(){
    int T=read();
    int oo=1;
    while(T--){
            ans=0;flag=0;
        for(int i=0;i<4;i++){
            scanf("%s",mp[i]);
            for(int j=0;j<4;j++){
                if(mp[i][j]=='*')ans++;
            }
        }
        printf("Case #%d:\n",oo++);
     dfs(0,0,0);
    }
 return 0;
}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/4924261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值