A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6 8
Sample Output
Case 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2
思路:DFS常规题,首位是1,找出2~n中与1相加是素数的数放在第二位,再在剩下的数中找出与第二位之和为素数的数,一直DFS下去,判断一下最后一个数与1相加是否为素数,是则输出整行数。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],vis[100100];
int n;
int prim(int x)//x是否为素数
{
for(int i=2;i*i<=x;i++)
if(x%i==0)
return 0;
return 1;
}
void dfs(int c)//从1开始搜索
{
if(c==n&&prim(num[n-1]+1))//如果排到了最后一位且与第一位的和满足条件
{
printf("1");//保证第一位是1;
for(int i=1;i<n;i++)//实际从num[]的第二位输出
printf(" %d",num[i]);//一直到n-1位
printf("\n");
}
if(c==n) return ;
for(int i=2;i<=n;i++)
{
if(!vis[i]&&prim(num[c-1]+i))//满足与上一位和为素数,且数字没有被使用过
{
num[c]=i;//放入这个满足条件的数
vis[i]=1;//标记
dfs(c+1);//搜索下一位
vis[i]=0;
}
}
}
int main()
{
int k=1;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));//每一次的n,都要讲标记数组清零
num[0]=1;
vis[1]=1;//标记1被使用
printf("Case %d:\n",k++);
dfs(1);
printf("\n");
}
return 0;
}