码迷,mamicode.com
首页 > 其他好文 > 详细

uva208递归枚举

时间:2015-07-31 10:45:08      阅读:84      评论:0      收藏: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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

uva208递归枚举

标签:

原文地址:http://blog.csdn.net/a197p/article/details/47165033

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!