鱼C论坛

 找回密码
 立即注册
查看: 2570|回复: 1

[技术交流] 033:Prime RingProblem素环问题

[复制链接]
发表于 2018-2-17 14:52:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Problem Description

A ring is composeof n circles as shown in diagram. Put natural number 1, 2, ..., n into eachcircle separately, and the sum of numbers in two adjacent circles should be aprime.
Note: the number of first circle should always be 1.

环是组合成n个圆圈所示的图。把自然数1,2,...,n为进分别各圈,和数字中的两个相邻圈之和应该是一个素数。

注意:第一圈的数量始终应为1。

Input

n (0 < n <20).

Output

The output formatis shown as sample below. Each row represents a series of circle numbers in thering beginning from 1 clockwisely and anticlockwisely. The order of numbersmust 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.

输出格式如下图所示的样品。每一行代表一个系列的圆形数字的环从1开始顺时针和按逆时针方向。号的顺序,必须满足上述要求。在字典顺序打印解决方案。

请你写一个程序,完成上述过程。

打印每个案例后,一个空行。

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

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-17 14:53:53 | 显示全部楼层
计算规模并不庞大,使用dfs和递归最容易不过

  1. #include<cstdio>
  2. #include<cstring>
  3. #define N 20

  4. using namespace std;
  5. int ans[N];
  6. bool hash[N];
  7. int n;
  8. int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41};
  9. bool judge(int x)
  10. {
  11.         for(int i=0;i<13;i++)
  12.                 if(prime[i]==x)
  13.                         return true;
  14.         return false;
  15. }
  16. void check()
  17. {
  18.         if(judge(ans[n]+ans[1])==false)
  19.                 return ;
  20.         for(int i=1;i<=n;i++)
  21.         {
  22.                 if(i!=1)
  23.                         printf(" ");
  24.                 printf("%d",ans[i]);
  25.         }       
  26.         printf("\n");
  27. }
  28. void DFS(int num)
  29. {
  30.         if(num>1)
  31.                 if(judge(ans[num]+ans[num-1])==false)
  32.                         return ;
  33.         if(num==n)
  34.         {
  35.                 check();
  36.                 return ;
  37.         }
  38.         for(int i=2;i<=n;i++)
  39.         {
  40.                 if(hash[i]==false)
  41.                 {
  42.                         hash[i]=true;
  43.                         ans[num+1]=i;
  44.                         DFS(num+1);
  45.                         hash[i]=false;     //关键!!!回溯!!!
  46.                 }
  47.         }
  48.        
  49. }
  50. int main()
  51. {
  52.         int cas=0;
  53.         while(scanf("%d",&n)!=EOF)
  54.         {
  55.                 cas++;
  56.                 for(int i=0;i<N;i++)
  57.                         hash[i]=false;
  58.                 ans[1]=1;
  59.                 printf("Case %d:\n",cas);
  60.                 hash[1]=true;
  61.                 DFS(1);
  62.                 printf("\n");
  63.         }
  64.         return 0;
  65. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 11:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表