DFS hdu 1016
2666 ワード
http://acm.hdu.edu.cn/showproblem.php?pid=1016



#include <iostream>
using namespace std;
int a[30],n,used[40];
int is_prime(int x)
{
for(int i=2;i<x; i++)
if(x%i==0) return 0;
return 1;
}
void dfs(int cur)
{
int i;
if(cur==n && is_prime(a[n]+a[1]))
{
for(i=1;i<n;i++) cout<<a[i]<<" ";
cout<<a[n]<<"
";
return;
}
for(i=2;i<=n;i++)
{
if(used[i]==0 && is_prime(a[cur]+i))
{ a[cur+1]=i ; used[i]=1 ; dfs(cur+1);used[i]=0 ;}
}
}
int main( )
{ int c=1;
while(cin>>n)
{
memset(used,0,sizeof(used)) ;
a[1]=1; used[1]=1;
cout<<"Case "<<c++<<":"<<endl ;
dfs(1);
cout<<endl;
}
}

