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. ”危险“的RESTRICT与GCC的编译优化(编程者对编译器所做的一个“承诺”:使用restrict修饰过的指针,它所指向的内容只能经由该指针修改)

    restrict是C99标准中新添加的关键字,对于从C89标准开始起步学习C语言的同学来说(包括我),第一次看到restrict还是相当陌生的.Wikipedia给出的解释如下: In the C p ...

  2. Android系统驱动【转】

    本文转载自:http://www.hovercool.com/en/%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97#a_.E5.9B.9B ...

  3. asp.net website 单独编译某个页面,连带编译app_code

    选中某一个页面,然后右键build page

  4. Api接口服务的设计和安全解决方案

    这个涉及到两个方面问题:一个是接口访问认证问题,主要解决谁可以使用接口(用户登录验证.来路验证)一个是数据数据传输安全,主要解决接口数据被监听(HTTPS安全传输.敏感内容加密.数字签名) 普通网站应 ...

  5. python之--初始面向对象

    阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 类命名空间与对象.实例的命名空间 类的组合用法 初识面向对象小结 面向对象的三大特性 继承 多态 封装 ...

  6. Dragon Balls[HDU3635]

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. 三种排序方法(c语言)

    #include "stdio.h" void main() {void read_data(int a[],int n); void write_data(int a[],int ...

  8. Android 对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming),请尊重他人的辛勤劳动成果,谢谢! 随着移动互联网的快速发展,它已经和我们的生活息息相关了,在 ...

  9. mysql update join优化update in查询效率

    数据库版本:5.6.16 update in 修改数据,结果执行时间过慢,一直不出结果. SQL语句及执行计划如下: UPDATE erp_order_extra SET last_time=1231 ...

  10. PHP-输入账号密码进入网页

    1. 2. 3. 4.(itcast.html步骤)随便一个本地网页代替即可