hdu 1426

题意:中文题,此处省。

ac代码:

View Code
#include<iostream>
using namespace std;

int map[12][12];
int k; 
int foat;

struct tree
{
    int i;
    int j;
}node[90];


int check(struct tree n,int p)
{
    int i,j;
    int x,y;

    for(i=1;i<=9;i++)//判断行与列是否已经存在p
    {
        if(map[n.i][i]==p||map[i][n.j]==p)
            return 0;
    }


    if(n.i<4)//以下的一系列if() else语句都是用来找9个小格的小方块中坐上角的坐标
    {
        if(n.j<4)
        {
            x=1;y=1;
        }
        else
            if(n.j<7)
            {
                x=1;y=4;
            }
            else
            {
                x=1;y=7;
            }
    }
    else
        if(n.i<7)
        {
            if(n.j<4)
            {
                x=4;y=1;
            }
            else
                if(n.j<7)
                {
                    x=4;y=4;
                }
                else
                {
                    x=4;y=7;
                }
        }
        else
        {
            if(n.j<4)
            {
                x=7;y=1;
            }
            else
                if(n.j<7)
                {
                    x=7;y=4;
                }
                else
                {
                    x=7;y=7;
                }
        }


        for(i=x;i<=x+2;i++)//判断小方块中是否存在p
        {
            for(j=y;j<=y+2;j++)
            {
                if(map[i][j]==p)
                    return 0;
            }
        }

        return 1;
}



    


void dfs(int sum)
{
    if(sum==k)
    {
        foat=1;

        for(int i=1;i<=9;i++)//全部填满后,必须在这输出,否则回溯后,数据还原。
        {
            for( int j=1;j<=8;j++)
            {
                cout<<map[i][j]<<" ";
            }
            cout<<map[i][9]<<endl;
        }

        return ;
    }
    else
    {
        for(int i=1;i<=9;i++)
        {
            if(foat==1)
                break;
            if(check(node[sum],i))
            {
                map[node[sum].i][node[sum].j]=i;//填格
                dfs(sum+1);
                map[node[sum].i][node[sum].j]=-1;//回溯
            }
        }
    }
    return ;
}


int main()
{
    char ch;
    int c=0;

    while(cin>>ch)
    { 
        memset(node,0,sizeof(node));
        
        int i,j;
        for(i=1;i<=9;i++)
        {
            for(j=1;j<=9;j++)
            {
                if(i!=1||j!=1)//因为第一个小格已经输入,所以不需再次输入
                {
                    cin>>ch;
                }

                if(ch!='?')
                    map[i][j]=ch-'0';
                else
                {
                    node[k].i=i;
                    node[k].j=j;
                    k++;
                    map[i][j]=-1;
                }
            }
        }

        if(c++)
            cout<<endl;

        foat=0;
        k=1; 
        dfs(1);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zgfailmr/archive/2012/09/11/2680756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值