Prime Ring Problem (素环问题-路径)

Prime Ring Problem

 HDU - 1016 

第一次竟然把a数组打成 bool 

Fear

I'm scared to death

#include<bits/stdc++.h>
using namespace std;
#define maxn 500
bool isprime[maxn],vis[maxn];
int a[maxn];
int n;
void prime()
{
    memset(vis,0,sizeof(vis));
    isprime[0]=isprime[1]=1;
    for(int i=2; i<maxn; i++)
        for(int j=i*i; j<maxn; j+=i)
            isprime[j]=1;
}
void dfs(int step)
{
    if(step>n&&!isprime[a[step-1]+1])
    {
        for(int j=1; j<n; j++)
            cout<<a[j]<<" ";
        cout<<a[n]<<endl;
    }
    for(int i=2; i<=n; i++)
    {
        a[step]=i;
        if(!isprime[a[step]+a[step-1]]&&!vis[i])
        {
            vis[i]=1;
            dfs(step+1);
            vis[i]=0;
        }
    }
}
int main()
{
    prime();
    int cnt =1;
    while(cin>>n)
    {
        cout<<"Case "<<cnt++<<":"<<endl;
        memset(vis,0,sizeof(vis));
        a[1]=1;
        dfs(2);
        cout<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/81367100