uva208递归枚举

9 篇文章 0 订阅
#include<cstdio>
#include<cstring>
int n;
int sett[31];
int find2(int x)
{
    return sett[x] = (x==sett[x])?x:find2(sett[x]);
}
int mapp[25][25];
int B[25];
bool  used[25];
int degree=0;
void print(int cur,int *B,bool * used,int k)
{
    if(cur == n) {
            printf("1");
        for(int i=1;i<k;i++) printf(" %d",B[i]); printf("\n");
        degree++;
        return ;
    }
    else {
        for(int j=1;j<=20;j++){
             if(mapp[cur][j]==1 &&  !used[j]) {
//                    printf("*%d*",used[2]);
                used[j]=true;
                B[k]=j;
                k++;
                print(j,B,used,k);
                used[j]=false;k--;

            }
        }
    }

}
int main()
{
    int x,y,cases=1;
    while(~scanf("%d",&n)){
            memset(mapp,0,sizeof(mapp));
        for(int i=0;i<=30;i++) sett[i]=i;
        /**数组越界wrong了!!!千万注意不要越界*/
    while(scanf("%d%d",&x,&y))
    {
        mapp[x][y]=1;
        mapp[y][x]=1;
        if(x==0&&y==0) break;
        int fx = find2(x);
        int fy = find2(y);
        if(fx > fy) sett[fx]=fy;
        else sett[fy]=fx;


    }
//    for(int i=1;i<=6;i++){
//    for(int j=1;j<=6;j++){
//        printf("%d ",mapp[i][j]);
//    }
//    printf("\n");
//    }
        printf("CASE %d:\n",cases++);
         if(find2(1) == find2(n)) {
            memset(used,false,sizeof(used));
//         memset(B,0,);
         used[1]=1;

         degree=0;
        print(1,B,used,1);
        printf("There are %d routes from the firestation to streetcorner %d.\n",degree,n);
         }
         else {
printf("There are 0 routes from the firestation to streetcorner %d.\n",n);
         }


    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值