hdu 1016(dfs基础)

题意:输入一个数n,把1到n的自然数放到一个环里,保证相邻的两个数的和是素数。(1和n

思路:素数打表+深搜


#include 
#include 
#include 
#include 
using namespace std;
int a[25],b[45]= {0},n;
int isprime[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};//素数打表
void dfs(int k)
{
    if(k==n+1&&isprime[a[n]+a[1]])
    {
        for(int i=1; i>n)
    {
        printf("Case %d:\n",++casen);
        a[1]=1;
        dfs(2);
        printf("\n");
        memset(a,0,sizeof(a));
    }
    return 0;
}


你可能感兴趣的:(dfs,模板题,经典题,离线处理)