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

    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[5];
    int p[5][5];
    int tx[20];
    int ty[20];
    int tot = 0;
    int flag;
    int vis[5];
    int check()
    {
        for(int i=0;i<4;i++)
        {
            vis[1]=vis[2]=vis[3]=vis[4]=0;
            for(int j=0;j<4;j++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
        for(int j=0;j<4;j++)
        {
            vis[1]=vis[2]=vis[3]=vis[4]=0;
            for(int i=0;i<4;i++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
        vis[1]=vis[2]=vis[3]=vis[4]=0;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
    
        vis[1]=vis[2]=vis[3]=vis[4]=0;
        for(int i=2;i<4;i++)
        {
            for(int j=0;j<2;j++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
        vis[1]=vis[2]=vis[3]=vis[4]=0;
        for(int i=0;i<2;i++)
        {
            for(int j=2;j<4;j++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
        vis[1]=vis[2]=vis[3]=vis[4]=0;
        for(int i=2;i<4;i++)
        {
            for(int j=2;j<4;j++)
            {
                if(p[i][j]==0)continue;
                if(vis[p[i][j]])return 0;
                vis[p[i][j]]=1;
            }
        }
        return 1;
    }
    void dfs(int x)
    {
        if(flag)return;
        if(x==tot){
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
                printf("%d",p[i][j]);
            printf("
    ");
        }
        flag=1;
        return;}
        for(int i=1;i<=4;i++)
        {
            p[tx[x]][ty[x]]=i;
            if(check())
                dfs(x+1);
            p[tx[x]][ty[x]]=0;
        }
    }
    int main()
    {
        int t;scanf("%d",&t);
        for(int cas = 1;cas <= t;cas++)
        {
            tot = 0;
            flag = 0;
            for(int i=0;i<4;i++)
                cin>>s[i];
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++)
                    if(s[i][j]=='*')
                        p[i][j]=0;
                    else
                        p[i][j]=s[i][j]-'0';
    
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++)
                    if(p[i][j]==0)
                    {
                        tx[tot]=i;
                        ty[tot]=j;
                        tot++;
                    }
            printf("Case #%d:
    ",cas);
            dfs(0);
        }
    }
  • 相关阅读:
    IP是什么 DNS 域名与IP有什么不同
    空间、域名与IP之间的关系?
    杨学明老师为深圳某上市企业提供《软件测试技术》内训服务!
    共创力与某上市企业合作的第三期咨询项目正式启动!
    2017.7.28~29,热烈庆祝杨学明老师《研发项目管理》杭州公开课成功举办!
    2017年7月22日~23日,深圳市共创力为某上市企业提供整机设计工程内训服务!
    深圳市共创力咨询第二期“总裁直通车”成功举办!
    阿里巴巴产品需求工程师的三个层次
    共创力咨询推出“总裁直通车”服务!
    2017年7月7日~8日,杨学明老师为深圳蛇口某企业内训课程服务!
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4899360.html
Copyright © 2011-2022 走看看