zoukankan      html  css  js  c++  java
  • 搜索----hdu 5547

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5547

    数独,保证每一行每一列都有1,2,3,4

    还有4 个2 * 2的小方块儿里也必须是1,2,3,4

    输入:

    3
    ****
    2341
    4123
    3214
    *243
    *312
    *421
    *134
    *41*
    **3*
    2*41
    4*2*

    输出:

    Case #1:
    1432
    2341
    4123
    3214
    Case #2:
    1243
    4312
    3421
    2134
    Case #3:
    3412
    1234
    2341
    4123
    #include<stdio.h>  
    #include<string.h>  
    using namespace std;  
    char a[100][100];  
    int panduan(int row,int col)  
    {  
        for(int i=0;i<4;i++)  
        {  
            if(a[row][i]==a[row][col]&&i!=col)  
            return 0;  
        }  ///判断该数字是否可以存在这一行里
        for(int i=0;i<4;i++)  
        {  
            if(a[i][col]==a[row][col]&&i!=row)  
            return 0;  
        }  ///判断该数字是否可以存在这一列里
        int mrow=row;  
        int mcol=col;  
        if(row%2==1)row-=1;  
        if(col%2==1)col-=1;  
        for(int i=row;i<=row+1;i++)  
        {  
            for(int j=col;j<=col+1;j++)  
            {  
                {  
                    if(a[i][j]==a[mrow][mcol]&&i!=mrow&&j!=mcol)return 0;  
                }  
            }  
        }  ///判断2*2的小方框里是不是1,2,3,4
        return 1;  
    }  
    void dfs(int cur)  
    {  
        
        if(cur==4*4)///如果到达最后一个点则输出全部
        {  
            for(int i=0;i<4;i++)  
            {  
                for(int j=0;j<4;j++)  
                {  
                    printf("%c",a[i][j]);  
                }  
                printf("
    ");  
            }  
            return ;  
        }  
        int row=cur/4;///
        int col=cur%4;  ///
        if(a[row][col]=='*')  
        {  
            for(int j=1;j<=4;j++)  ///吧1,2,3,4每个数都试一遍
            {  
                a[row][col]=j+'0';  
                if(panduan(row,col)==1)  ///符合条件则进入下一个点
                {  
                    dfs(cur+1);  
                }  
                a[row][col]='*';  ///取消标记
            }  
        }  
        else  
        {  
            dfs(cur+1);  
        }  
    }  
    int main()  
    {  
        int kase=0;  
        int t;  
        scanf("%d",&t);  
        while(t--)  
        {  
            for(int i=0;i<4;i++)  
            {  
                scanf("%s",a[i]);  
            }  
            printf("Case #%d:
    ",++kase);  
            dfs(0);  
        }  
        return 0;  
    }  
  • 相关阅读:
    [1041] XX easy problem
    [1027] 火焰纹章
    省赛选拔赛解题报告
    二维DP hdu 1421 搬寝室问题
    最长斐波那契子序列选取(离散化 + 二分 + DP)
    两个有序链表合成一个有序链表
    CodeForce 608B Hamming Distance Sum
    CodeForce 607A&&608C Chain Reaction
    (DP)最大价值事件序列选取
    面向对象>>>抽象类>>>鸭子类型>>>封装
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/5738337.html
Copyright © 2011-2022 走看看