zoukankan      html  css  js  c++  java
  • 2015南阳CCPC H

    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
    ")
    #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:
    ",oo++);
         dfs(0,0,0);
        }
     return 0;
    }
    代码
  • 相关阅读:
    利用ganymed-ssh2远程执行其它Linux机器上的shell命令
    ZooKeeper 笔记(4) 实战应用之【消除单点故障】
    ZooKeeper 笔记(3) 实战应用之【统一配置管理】
    ZooKeeper 笔记(2) 监听数据变化
    ZooKeeper 笔记(1) 安装部署及hello world
    intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理
    hadoop: hive 1.2.0 在mac机上的安装与配置
    mac 下卸载mysql的方法
    mac OS X Yosemite 上编译hadoop 2.6.0/2.7.0及TEZ 0.5.2/0.7.0 注意事项
    hadoop: hbase1.0.1.1 伪分布安装
  • 原文地址:https://www.cnblogs.com/zxhl/p/4924261.html
Copyright © 2011-2022 走看看