Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25156    Accepted Submission(s): 11234

Problem Description
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
  1. 6
  2. 8
 
Sample Output
  1. Case 1:
  2. 1 4 3 2 5 6
  3. 1 6 5 2 3 4
  4. Case 2:
  5. 1 2 3 8 5 6 7 4
  6. 1 2 5 8 3 4 7 6
  7. 1 4 7 6 5 8 3 2
  8. 1 6 7 4 3 8 5 2
  1. </pre><pre name="code" class="cpp">#include<cstdio>
  2. #include<stdlib.h>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. int date[25]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
  10. int ans[25];
  11. int n, cnt;
  12. int flag;
  13.  
  14. void dfs(int step)
  15. {
  16. int temp;
  17. if(step==n) //深搜究竟时注意推断首尾是否符合要求
  18. {
  19. flag=1;
  20. temp = ans[0]+ans[n-1];
  21. if(flag)
  22. {
  23. for(int i=2; i*i<=temp; i++)
  24. if(temp%i==0)
  25. {
  26. flag=0;
  27. break;
  28. }
  29. }
  30. if(flag)
  31. {
  32. printf("%d", ans[0]);
  33. for(int i=1; i<n; i++)
  34. printf(" %d", ans[i]);
  35. printf("\n");
  36. }
  37. }
  38. else
  39. {
  40. for(int i=1; i<n; i++)
  41. {
  42. if(date[i])
  43. {
  44. temp=ans[cnt-1]+date[i];
  45. flag=1;
  46. for(int j=2; j*j<=temp; j++)
  47. {
  48. if(temp%j==0)
  49. {
  50. flag=0;
  51. break;
  52. }
  53. }
  54. if(flag)
  55. {
  56. ans[cnt++]=date[i];
  57. date[i]=0;
  58. dfs(step+1);
  59. date[i]=ans[--cnt]; //记得将数据的还原处理
  60. ans[cnt]=0;
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. int main()
  68. {
  69. int count=1;
  70. while(scanf("%d", &n)!=EOF)
  71. {
  72. memset(ans, 0, sizeof(ans));
  73. cnt=1;
  74. ans[0]=1;
  75. printf("Case %d:\n",count++);
  76. dfs(1); //从1開始进入搜索
  77. printf("\n"); //每次输出后记得加个换行
  78. }
  79.  
  80. return 0;
  81. }

HDU 1016:Prime Ring Problem的更多相关文章

  1. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. 九度oj 题目1459:Prime ring problem

    题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...

  3. DFS:Prime Ring Problem(素数环)

    解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...

  4. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  6. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  8. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

随机推荐

  1. Oracle初始安装内存设置参考

      预备知识 shared memory:共享内存段: 一个内存区域,可以被不同的进程读取.oracle使用它来构成sga.oracle使用以下三种方法来创建一个sga区: 1. 使用单个共享内存段. ...

  2. AC-PC线(前联合-后联合线)

    下面利用一张大脑矢状面(侧视图)来描述ac-pc的空间位置关系.前联合用红色点表示,后联合用黄色表示. 在Talairach 模板的官方文档中,AC-PC线从前联合AC的表面出发,延伸到后联合PC的中 ...

  3. Dialog 样式 主题 标题 背景 使用【总结】

    最重要的是这两行代码 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题,需在setContentView之前设置 ...

  4. 转:美团Android资源混淆保护实践

    转自:http://tech.meituan.com/mt-android-resource-obfuscation.html 前言 Android应用中的APK安全性一直遭人诟病,市面上充斥着各种被 ...

  5. [ES6] 02. Traceur compiler and Grunt

    Local Install:  npm install -g traceur npm install grunt-contrib-watch npm install grunt-traceur-lat ...

  6. JAVA设计模式之 訪问者模式【Visitor Pattern】

    一.概述 訪问者模式是一种较为复杂的行为型设计模式,它包括訪问者和被訪问元素两个主要组成部分.这些被訪问的元素通常具有不同的类型,且不同的訪问者能够对它们进行不同的訪问操作.在使用訪问者模式时,被訪问 ...

  7. 符号三角形_hdu_2510(深搜).java

    http://acm.hdu.edu.cn/showproblem.php?pid=2510 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. 算法笔记_036:预排序(Java)

    目录 1 问题描述 2 解决方案 2.1 检验数组中元素的唯一性 2.2 模式计算   1 问题描述 在计算机科学中,预排序是一种很古老的思想.实际上,对于排序算法的兴趣很大程度上是因为这样一个事实: ...

  9. QtGui.QGridLayout

    The most universal layout class is the grid layout. This layout divides the space into rows and colu ...

  10. SqlServer聚合函数

    聚合函数对一组值计算后返回单个值.除了count(统计项数)函数以外,其他的聚合函数在计算式都会忽略空值(null).所有的聚合函数均为确定性函数.即任何时候使用一组相同的输入值调用聚合函数执行后的返 ...