Prime Ring Problem

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

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
 
题意   给你一个n  对1 ... n   n个数排序 1必须在第一位 两个数相加要为素数(1与最后一个数相加也必须为素数) 输出所有满足上述的序列。
解析 我们先1~n分别求出与它相加为素数的数存进数组,然后就相当于建了一个图 从1出发 一层一层向下推 基础DFS 然后在回溯一下 记录路径就好了
AC代码
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <queue>
  10. #include <vector>
  11. using namespace std;
  12. const int maxn= ;
  13. const int maxm= 1e4+;
  14. const int inf = 0x3f3f3f3f;
  15. typedef long long ll;
  16. int visit[maxn],order[maxn];
  17. vector<int> v[maxn];
  18. int n,cnt;
  19. int isPrime(int num )
  20. {
  21. int tmp =sqrt( num);
  22. for(int i= ;i <=tmp; i++)
  23. if(num %i== )
  24. return ;
  25. return ;
  26. }
  27. void dfs(int x)
  28. {
  29. visit[x]=; //标记访问过
  30. order[cnt++]=x; //记录路径
  31. for(int i=;i<v[x].size();i++) //df搜边
  32. {
  33. if(visit[v[x][i]]==)
  34. {
  35. dfs(v[x][i]);
  36. }
  37. }
  38. visit[x]=; //回溯
  39. if(cnt==n&&isPrime(x+)) //长度为n且最后一个数与1相加为素数
  40. {
  41. for(int i=;i<cnt;i++)
  42. {
  43. if(i==cnt-)
  44. printf("%d\n",order[i]);
  45. else
  46. printf("%d ",order[i]);
  47. }
  48. }
  49. cnt--;
  50. }
  51. int main()
  52. {
  53. int kase=;
  54. while(scanf("%d",&n)!=EOF)
  55. {
  56. for(int i=;i<=n;i++)
  57. v[i].clear();
  58. for(int i=;i<=n;i++)
  59. {
  60. for(int j=;j<=n;j++)
  61. {
  62. if(isPrime(i+j)==&&i!=j) //保存能够与i相加为素数的数
  63. v[i].push_back(j);
  64. }
  65. }
  66. // for(int i=1;i<=n;i++)
  67. // {
  68. // cout<<i;
  69. // for(int j=0;j<v[i].size();j++)
  70. // printf(" %d",v[i][j]);
  71. // cout<<endl;
  72. // }
  73. memset(visit,,sizeof(visit)); //初始化访问数组 0未访问过
  74. printf("Case %d:\n",kase++);
  75. cnt=; //路径长度初始化
  76. dfs();
  77. printf("\n");
  78. }
  79. }

HDU1016 DFS+回溯(保存路径)的更多相关文章

  1. HDU1016 Prime Ring Problem(DFS回溯)

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

  2. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  3. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  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 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. iOS SDAutoLayout图文混排-共享

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #526eda } span.s1 { color: #4dbf5 ...

  2. bzoj 2753: [SCOI2012]滑雪与时间胶囊

    Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...

  3. Spring之DAO一

    前面博客把bean.aop简单了解了一下,今天主要是了解Spring中DAO层,如果使用传统的JDBC时需要创建连接.打开.执行sql.关闭连接这一系列的步骤,Spring框架对JDBC进行了封装,我 ...

  4. Macaca环境搭建踩坑总结

    1.使用命令 npm i macaca-android -g 安装一直不成功,使用Macaca  doctor 一直没有显示出android C:\Users\ABC>npm i macaca- ...

  5. JavaScript的DOM编程--11--插入节点

    插入节点: 1). insertBefore(): 把一个给定节点插入到一个给定元素节点的给定子节点的前面 var reference = element.insertBefore(newNode,t ...

  6. JAVA个人理解

    为了找到别人写的好文章,先分享下自己的知识,找找感觉路线. 学java前接触的c,后来转向java.第一个照面理解的就是面向对象,没想到让我想了好多年.当时有个负责任的老师说面向对象这个词具体释义众说 ...

  7. 线程安全Dictionary

    public abstract class ReadFreeCache<TKey, TValue> { protected ReadFreeCache() : this(null) { } ...

  8. class java.awt.HeadlessException : No X11 DISPLAY variable was set, but this program performed an operation which requires it.

    今天上午打印回单功能发布到测试环境,报了: class java.awt.HeadlessException : No X11 DISPLAY variable was set, but this p ...

  9. 中国孩子的micro:bit:TurnipBit自制小乐器教程实例

    孩子们是最贪玩的也是最聪明的,因此在过去的数年中,市面上出现了不少寓教于乐的理工科知识(STEM)学习新方法.如今这类产品中又有了一名新成员,TPYBoard重磅推出一款针对小白.中小学生的可编程计算 ...

  10. springboot 注册服务注册中心(zk)的两种方式

    在使用springboot进行开发的过程中,我们经常需要处理这样的场景:在服务启动的时候,需要向服务注册中心(例如zk)注册服务状态,以便当服务状态改变的时候,可以故障摘除和负载均衡. 我遇到过两种注 ...