Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42600    Accepted Submission(s): 18885

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
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

 
Source

题目链接:HDU 1016

经典的DFS回溯题目,以前一直想做来着,但是不懂回溯搜索,现在类似的一些题还是挺简单的……,这题用输出外挂可以优化到200+MS,题意是把1-n中所有自然数全部排完才能算一个环,刚开始搞错了输出爆炸……

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<string>
  8. #include<deque>
  9. #include<stack>
  10. #include<cmath>
  11. #include<queue>
  12. #include<set>
  13. #include<map>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MM(x,y) memset(x,y,sizeof(x))
  17. #define LC(x) (x<<1)
  18. #define RC(x) ((x<<1)+1)
  19. #define MID(x,y) ((x+y)>>1)
  20. typedef pair<int,int> pii;
  21. typedef long long LL;
  22. const double PI=acos(-1.0);
  23. const int N=50;
  24. int prime[N];
  25. int pos[N],vis[N];
  26. int n;
  27. void Out(int a)
  28. {
  29. if(a>9)
  30. Out(a/10);
  31. putchar(a%10+'0');
  32. }
  33. inline bool check()
  34. {
  35. for (int i=1; i<=n; ++i)
  36. {
  37. if(!vis[i])
  38. return false;
  39. }
  40. return true;
  41. }
  42. void dfs(int now)
  43. {
  44. if(now==n)
  45. {
  46. if(prime[pos[now]+pos[1]]&&check())
  47. {
  48. for (int i=1; i<=n; ++i)
  49. {
  50. Out(pos[i]);
  51. putchar(i==n?'\n':' ');
  52. }
  53. }
  54. return ;
  55. }
  56. for (int i=2; i<=n; ++i)
  57. {
  58. if(!vis[i]&&prime[i+pos[now]]&&now<=n)
  59. {
  60. vis[i]=1;
  61. pos[now+1]=i;
  62. dfs(now+1);
  63. pos[now+1]=0;
  64. vis[i]=0;
  65. }
  66. }
  67. }
  68. int main(void)
  69. {
  70. int i,j;
  71. for (i=0; i<N; ++i)
  72. prime[i]=1;
  73. prime[1]=0;
  74. for (i=2; i<N; ++i)
  75. for (j=2; j*i<N; ++j)
  76. prime[i*j]=0;
  77. int tcase=0,m;
  78. while (~scanf("%d",&n))
  79. {
  80. MM(pos,0);
  81. vis[1]=1;
  82. pos[1]=1;
  83. printf("Case %d:\n",++tcase);
  84. dfs(1);
  85. putchar('\n');
  86. }
  87. return 0;
  88. }

HDU 1016 Prime Ring Problem(经典DFS+回溯)的更多相关文章

  1. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  2. hdu 1016 Prime Ring Problem(dfs)

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

  3. hdu 1016 Prime Ring Problem (dfs)

    一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...

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

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

  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. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  8. HDU 1016 Prime Ring Problem (回溯法)

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

  9. HDU 1016 Prime Ring Problem (DFS)

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

随机推荐

  1. java获取tomcat路径

    获取tomcat路径 String savePath3 = System.getProperty("catalina.home"); E:\apache-tomcat-7.0.63 ...

  2. 【USACO】clocks 遇到各种问题 最后还是参考别人的思路

    //放在USACO上一直通不过 不知道哪里出了问题 输出的n总是等于1 但是BFS递归的次数是对的 <----这个问题解决了 局部变量压入queue中返回就是对的了 #include<io ...

  3. XML Parser Error on line 1: 前言中不允许有内容, Mybatis 生成代码

    使用用notepad++打开xml文件,然后在菜单“格式”中选择“以UTF-8无BOM格式编码”,保存.

  4. Ubuntu下中文显示乱码

    转自:http://jingyan.baidu.com/article/915fc414c9d2ef51384b204c.html 装好了Ubuntu 13.10 后,打开部分Windows下的txt ...

  5. M方法和D方法的区别

    M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...

  6. Visual Studio 2015 RC中的ASP.NET新特性和问题修正

    (此文章同时发表在本人微信公众号"dotNET每日精华文章") 微软在Build大会上发布了Visual Studio 2015 RC,这也预示着Visual Studio 201 ...

  7. Struts2标签实现for循环

    感悟:但是不建议使用这种方法,按照MVC框架的思想 ,应该把业务更多放在后台.前台尽量只进行数据展示. 转自:http://blog.csdn.net/guandajian/article/detai ...

  8. 【Tyvj】1473校门外的树3 线段树/树状数组 <区间修改+单点访问>

    描述  校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,读入l,r表示在l ...

  9. 在虚拟机环境下,电脑间拷贝配置好的伪分布式Hadoop环境,出现namenode不能启动的问题!

    原因:在原来的电脑上配置伪分布的时候,已经将hostname与IP绑定了,所以拷贝到别的电脑的时候,重新启动的时候就会失败,因为新的电脑的IP不见得就和原来的电脑的IP一样!因为在不同的网络中,在NA ...

  10. 【HTML5】表单属性

    * autocomplete autocomplete 属性规定 form 或 input 域应该拥有自动完成功能. 注释:autocomplete 适用于 <form> 标签,以及以下类 ...