Prime Ring Problem

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

                                                                                                Total Submission(s): 37129    Accepted Submission(s): 16363

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这些数经过排列之后得到一个数列,数列中相邻的两个数和为素数(这里什么是素数就不说了),数列开始的第一位是1,并且此数列第一个数与最后一个数相加也为素数, 这道题中我们用a数组来记录得到的数据,v数组来标记对应的数字是否是用过。每次递归的时候我们传递的参数是找到的数字的个数,递归结束的标志就是找到了n位并且首位和末位的和是素数,或者找到了n位首位和末位和不为素数(代码中没有体现,可以自己添加),如果找到的数字个数不够n位,就进入for循环中,for循环枚举出每一个没有使用过的数字,同时判断这个没有使用过的数与前一位是否相加和为素数,如果条件满足,我们标记这个数已经使用过,同时也把这个数存入a数组,然后开始下一步搜索,但是下一步搜索之后就应该取消对上一个数的标记,这也就是回溯的精髓所在,就在这一段代码中 if(!v[i]&&pre(a[c-1]+i)) { a[c]=i; v[i]=1; dfs(c+1); v[i]=0;//回溯释放标志 } 如果这里不理解的话可以找个例子,假如我们在6以内寻找素数环,现在找到了1,4,3可能后面的搜索的分支结束了,但是我们还可以再次使用3,因为3完全可以在第四位,所以这一步我们需要释放标记,从而得到每一种可能继而判断。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],vis[100100];
int n;
int prim(int x)
{
for(int i=2;i*i<=x;i++)
if(x%i==0)
return 0;
return 1;
}
void dfs(int c)
{
if(c==n&&prim(num[n-1]+1))
{
printf("1");
for(int i=1;i<n;i++)
printf(" %d",num[i]);
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));
num[0]=1;
vis[1]=1;
printf("Case %d:\n",k++);
dfs(1);
printf("\n");
}
return 0;
}

hdoj--1016--Prime Ring Problem(递归回溯)的更多相关文章

  1. hdoj 1016 Prime Ring Problem

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

  2. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...

  3. HDOJ 1016 Prime Ring Problem素数环【深搜】

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

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

  10. hdu 1016 Prime Ring Problem(深度优先搜索)

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

随机推荐

  1. java痛苦学习之路[四]---关于struts2-convention-plugin使用

    一.struts2-convention-plugin配置文件具体解释 <constant name="struts.convention.actionConfigBuilder&qu ...

  2. poj--3630--Phone List(字典树+前缀判断)

    Phone List Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  3. Oracle数据库三种标准的备份方法

    Oracle数据库的三种标准的备份方法: 1.导出/导入(EXP/IMP). 2.热备份. 3.冷备份. 注释:导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一.导出/导入(Export/Imp ...

  4. DB2报“数据库日志已满”问题解决

    用控制中心直接改会比较容易一点,在数据库名称上点右键-->配置-->日志-->日志文件大小.主日志文件数.辅助日志文件数改大一点. 也可用命令行db2cmd db2 update d ...

  5. Javescript 面向对象编程 — 封装

    生成实例对象的原始模式 <script type="text/javascript"> var Cat={ name:'波斯猫', color:'White' } al ...

  6. node工具是是什么东西

    Node到底是个啥? Node是一个服务器端JavaScript解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不深我还不知道,不过确实不浅 最近写 ...

  7. js类的使用

    brush示例 以d3的一个brush进行叙述,示例见: https://bl.ocks.org/xunhanliu/6f0b46789842e9e19e6cfe9bd0b16806 应用情形: 当页 ...

  8. [caffe]网络各层参数设置

    数据层 数据层是模型最底层,提供提供数据输入和数据从Blobs转换成别的格式进行保存输出,通常数据预处理(减去均值,放大缩小,裁剪和镜像等)也在这一层设置参数实现. 参数设置: name: 名称 ty ...

  9. python_函数、局部变量与全局变量

    #函数优点:代码重用.保持一致性.可扩展性import time def logger(): """时间年-月-日 分""" time_fo ...

  10. React基础知识点全解

    •      propTypes.defaultProps 作为 properties 定义,也可以在组件外部通过键值对方式进行设置. •      设置组件初始的 state不支持 getIniti ...