回溯法-经典问题C程序

 回溯法的基本要点参见各算法书籍,这里给出两个简单运用的例子(数的全排列和八皇后问题)

数的全排列(含递归和非递归)
#include <stdio.h>
#define N 4
void arrange(int rec[],int used[],int depth)
{
    int i;
    if(depth>=N)
    {
        for(i=0;i<N;i++)
        {
            printf("%d",rec[i]);
        }
        printf("/n");
    }
    else
    {
        for(i=0;i<N;i++)
        {
            rec[depth]=i;
            if(used[i]==0)
            {
                used[i]=1;
                arrange(rec,used,depth+1);
                used[i]=0;
            }
            rec[depth]=0;
        }
    }
}

int selectnum(int used[],int start)
{
    int i;
    for(i=start;i<N;i++)
    {
        if(used[i]==0)
        {
            return i;
        }
    }
    return -1;
}

void arrange2(int rec[],int used[])
{
    int i,pos;
    for(i=0;i>-1;)
    {
        if(i>=N)
        {
            for(i=0;i<N;i++)
            {
                printf("%d",rec[i]);
            }
            printf("/n");
            i--;
        }
        pos=selectnum(used,rec[i]+1);
        if(pos!=-1)
        {
            used[pos]=1;
            rec[i]=pos;
            i++;
        }
        else
        {
            if(rec[i]>=0)
            {
                used[rec[i]]=0;
                rec[i]=-1;
                i--;
            }
            if(i>-1)
            {
                used[rec[i]]=0;
            }
        }
    }
}

int main()
{
    int i,rec[N],used[N];
    for(i=0;i<N;i++)
    {
        rec[i]=-1;
        used[i]=0;
    }
    arrange(rec,used,0);
    for(i=0;i<N;i++)
    {
        rec[i]=-1;
        used[i]=0;
    }
    printf("/n");
    arrange2(rec,used);
    return 0;
}

八皇后问题(
含递归和非递归
#include <stdio.h>
#define N 8

void eight_queen(int rec[],int used[],int s1[],int s2[],int depth)
{
    int i;
    if(depth>=N)
    {
        for(i=0;i<N;i++)
        {
            printf("%d ",rec[i]);
        }
        printf("/n");
    }
    else
    {
        for(i=0;i<N;i++)
        {
            rec[depth]=i;
            if(used[i]==0&&s1[i+(N-depth)]==0&&s2[i+depth]==0)
            {
                used[i]=1;
                s1[i+(N-depth)]=1;
                s2[i+depth]=1;
                eight_queen(rec,used,s1,s2,depth+1);
                used[i]=0;
                s1[i+(N-depth)]=0;
                s2[i+depth]=0;
            }
            rec[depth]=-1;
        }
    }
}

int selectnum(int used[],int s1[],int s2[],int start,int depth)
{
    int i;
    for(i=start;i<N;i++)
    {
        if(used[i]==0&&s1[i+(N-depth)]==0&&s2[i+depth]==0)
        {
            return i;
        }
    }
    return -1;
}

void eight_queen2(int rec[],int used[],int s1[],int s2[])
{
    int i,pos;
    for(i=0;i>-1;)
    {
        if(i>=N)
        {
            for(i=0;i<N;i++)
            {
                printf("%d ",rec[i]);
            }
            printf("/n");
            i--;
        }
        pos=selectnum(used,s1,s2,rec[i]+1,i);
        if(pos!=-1)
        {
            rec[i]=pos;
            used[pos]=1;
            s1[pos+(N-i)]=1;
            s2[pos+i]=1;
            i++;
        }
        else
        {
            if(rec[i]>=0)
            {
                used[rec[i]]=0;
                s1[rec[i]+(N-i)]=0;
                s2[rec[i]+i]=0;
                rec[i]=-1;
            }
            i--;
            if(i>=0)
            {
                used[rec[i]]=0;
                s1[rec[i]+(N-i)]=0;
                s2[rec[i]+i]=0;
            }
        }
    }
}

int main()
{
    int i;
    int rec[N],used[N],s1[2*N-1],s2[2*N-1];
    for(i=0;i<N;i++)
    {
        rec[i]=-1;
        used[i]=0;
    }
    for(i=0;i<2*N-1;i++)
    {
        s1[i]=0;
        s2[i]=0;
    }
    eight_queen(rec,used,s1,s2,0);
    printf("/n");
    for(i=0;i<N;i++)
    {
        rec[i]=-1;
        used[i]=0;
    }
    for(i=0;i<2*N-1;i++)
    {
        s1[i]=0;
        s2[i]=0;
    }
    eight_queen2(rec,used,s1,s2);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值