您的位置:首页 > 其它

杭电1016

2015-07-25 20:11 423 查看
上链接:杭电1016

题目大意:

从1到6排成一个环(可以不按顺序),满足相邻两个数的和为素数,输入1-20的数,输出可能存在的环

#include <cstdio>
#include <cstring>
using namespace std;
int c;
int num[21];
bool state[21];

bool IsPrime(int n)
{
if(n==2 || n==3 || n==5 || n==7 || n==11 || n==13 || n==17 || n==19 || n==23 || n==29 || n==31 || n==37 || n==39 )
return true;
else return false;
}

void dfs(int n)
{
if(n == c+1)
{
if(!IsPrime(1+num[n-1])) return;
printf("1");
for(int i=2; i<=c; ++i)
printf(" %d", num[i]);
printf("\n");
}
else
{
for(int i=2; i<=c; ++i)
{
if(state[i]) continue;
if(IsPrime(num[n-1]+i))
{
num
=i;
state[i]=1;
dfs(n+1);
state[i]=0;
}
}
}
}

int main()
{
int logo=0;
while(scanf("%d", &c)!=EOF)
{
++logo;
printf("Case %d:\n", logo);
memset(state,0,21);
num[1] = 1;
state[1] = true;
dfs(2);
printf("\n");
}

return 0;
}

注意问题:

1.深搜剪枝

2.注意输出格式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  杭电