uva208递归枚举

来源:互联网 发布:张成泽犬决 知乎 编辑:程序博客网 时间:2024/04/30 17:30
#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