zoukankan      html  css  js  c++  java
  • HDU 5547 4*4数独 http://acm.split.hdu.edu.cn/status.php

    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
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<math.h>
    #include<queue>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define N 8
    int g=0;
    char a[N][N];
    int qq(int x,int y)
    {
        for(int i=0; i<4; i++)
        {
            if(a[i][y]==a[x][y]&&x!=i)
                return 0;
            if(a[x][i]==a[x][y]&&y!=i)
                return 0;
        }
    
        int xx=x,yy=y;
        if(x%2) x--;
        if(y%2) y--;
        for(int i=x; i<=x+1; i++)
        {
            for(int j=y; j<=y+1; j++)
            {
                if(a[i][j]==a[xx][yy]&&(xx!=i&&yy!=j))
                    return 0;
            }
        }
    
        return 1;
    }
    void q(int ans)
    {
        if(g==1) return ;
        if(ans==16)
        {
            for(int i=0; i<4; i++)
            {
                for(int j=0;j<4;j++)
                    printf("%c",a[i][j]);
                printf("
    ");
            }
            g=1;
            return ;
        }
        int x=ans/4;
        int y=ans%4;
        if(a[x][y]=='*')
        {
            for(int j=1; j<=4; j++)
            {
                a[x][y]=j+'0';
                if(qq(x,y))
                {
                    q(ans+1);
                }
                a[x][y]='*';
            }
        }
        else
            q(ans+1);
    }
    int main()
    {
        int T,t=1;
        scanf("%d",&T);
        while(T--)
        {
            g=0;
            for(int i=0; i<4; i++)
                scanf("%s",a[i]);
            printf("Case #%d:
    ",t++);
            q(0);
        }
        return 0;
    }
  • 相关阅读:
    servlet
    过滤器
    拦截器
    logback
    hibernate(1)
    函数的关键字参数
    函数的不定长参数
    打印星形三角
    九九乘法表
    udp客户端收发数据流程
  • 原文地址:https://www.cnblogs.com/a719525932/p/5804079.html
Copyright © 2011-2022 走看看