zoukankan      html  css  js  c++  java
  • HDU 5547 暴力

    Sudoku

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1064    Accepted Submission(s): 362


    Problem 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(1T100)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
     
    题意:数独,横的、竖的、每个四分之一块不能有相同的数字。
    题解:开三个数组记录横的、竖的、四分之一块1、2、3、4出现的情况,标记一下。然后枚举每个位置,如果没有数字的话,if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)来确定填何数,如果有两个可以填,先跳过。
     
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    char s[5][5];
    int ans[5][5];
    int l[5][5];
    int r[5][5];
    int p[5][5];
    int judge(int x,int y)
    {
        if(x<2&&y<2) return 1;
        if(x>=2&&y<2) return 3;
        if(x<2&&y>=2) return 2;
        if(x>=2&&y>=2) return 4;
    }
    void solve()
    {
        for(int i = 0; i<4; i++)
            for(int j = 0; j<4; j++)
        {
            l[i][ans[i][j]] = 1;
        }
        for(int j = 0; j<4; j++)
            for(int i = 0; i<4; i++)
        {
            r[j][ans[i][j]] = 1;
        }
        for(int i = 0; i<4; i++)
        {
            for(int j = 0; j<4; j++)
            {
                p[judge(i,j)][ans[i][j]] = 1;
            }
        }
        int flag = 1;
        int count = 0;
        while(flag)
        {
            flag = 0;
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j<4; j++)
                {
                    count = 0;
                    if(ans[i][j]) continue;
                    for(int k = 1; k<=4; k++)
                    {
                        if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)
                            count++;
                    }
                    if(count == 1)
                    {
                        flag  = 1;
                        for(int k = 1; k<=4; k++)
                        {
                            if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)
                            {
                                ans[i][j] = k;
                                l[i][k] = 1;
                                r[j][k] = 1;
                                p[judge(i,j)][k] = 1;
                            }
                        }
                    }
                }
            }
        }
    }
    int main()
    {
        int t,kase = 0;
        scanf("%d",&t);
        while(t--)
        {
            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            memset(ans,0,sizeof(ans));
            memset(p,0,sizeof(p));
            for(int i = 0; i<4; i++)
            {
                scanf("%s",s[i]);
            }
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j<4; j++)
                {
                    if(s[i][j] == '*') ans[i][j] = 0;
                    else ans[i][j] = s[i][j] - '0';
                }
            }
            solve();
            printf("Case #%d:
    ",++kase);
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j<4; j++)
                {
                    printf("%d",ans[i][j]);
                }
                printf("
    ");
            }
        }
        return 0;
    }
     
     
  • 相关阅读:
    mycat安装
    docker注册&打包
    docker的使用场景和背景了解
    解析nohup java -jar xxx &
    透明度百分比和十六进制对应关系
    android get cpu rate
    Cordova插件开发
    VectorDrawable在Android中的配置
    APK反编译后添加日志
    apk重新签名
  • 原文地址:https://www.cnblogs.com/littlepear/p/5669174.html
Copyright © 2011-2022 走看看