码迷,mamicode.com
首页 > 其他好文 > 详细

2015南阳CCPC H - Sudoku 数独

时间:2015-10-30 20:28:55      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

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;
}
代码

 

2015南阳CCPC H - Sudoku 数独

标签:

原文地址:http://www.cnblogs.com/zxhl/p/4924261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!