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);
        }
    }
  • 相关阅读:
    让元素水平和垂直居中的方法总结
    a标签常见问题
    centos7的安装
    java面向对象-类的定义
    java内存划分
    java基础知识点-数组
    java基础知识点-重载
    第一章:服务器的组成知识点
    java 第二天运算符及录入函数Scanner
    java第一天 数据类型、变量的命名、类型的转换
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4899360.html
Copyright © 2011-2022 走看看