hdu1016_dfs

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int  visited[21];
int prime[40] ={0,1,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},n;
int a[21];
void dfs(int num){
   if(num==n&&prime[a[num-1]+a[0]]){
        for(int i=0;i<n-1;i++){
            printf("%d ",a[i]);
        }
        printf("%d\n",a[n-1]);
   }
   else{
    for(int i=2;i<=n;i++){
        if(visited[i]==0){
            if(prime[i+a[num-1]]){
                visited[i]=-1;
                a[num++]=i;
                dfs(num);
                visited[i]=0;
                num--;
            }
        }
    }
   }
}
int main()
{
    int num=0;
    while(~scanf("%d",&n)){
          num++;
            printf("Case %d:\n",num);
          memset( visited,0,sizeof(visited));
           a[0]=1;
           dfs(1);
            printf("\n");
    }
    return 0;
}

 

posted on 2019-02-27 20:11  jrz  阅读(80)  评论(0编辑  收藏  举报

导航