HDU 1016

#include
#include
int n,ans[21],isprime[]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0},vis[21];
void dfs(int step)
{
    int i;
    if(step==n+1)
    {
        if(isprime[ans[n]+1])
            for(i=1;i<=n;i++)
                printf("%d%c",ans[i],i==n?'\n':' ');
    }
    else
        for(i=2;i<=n;i++)
            if(!vis[i]&&isprime[i+ans[step-1]])
            {
                ans[step]=i;
                vis[i]=1;
                dfs(step+1);
                vis[i]=0;
            }
}
int main()
{
    int count;
    count=0;
    while(scanf("%d",&n)!=EOF)
    {
        count++;
        ans[1]=1;
        memset(vis,0,sizeof(vis));
        printf("Case %d:\n",count);
        dfs(2);
        printf("\n");
    }
} 

你可能感兴趣的:(HDU,Graph)