Prime Ring Problem

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

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
深度搜索....无压力;;
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int str[]={,,,,,,,,,,,,,,,,,,,};
int ans[]={};
int n,cnt; /*代表搜索的深度*/
bool flag;
/*可能需要剪枝*/
void dfs(int step)
{
int i,j,temp;
if(step==n) /*说明搜索到底了!*/
{
flag=true;
temp=ans[]+ans[n-]; //开头和结尾也要判断
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
printf("%d",ans[]);
for( i=;i<n;i++)
{
printf(" %d",ans[i]);
}
puts("");
}
}
else
{
for(i=;i<n;i++)
{
if(str[i])
{
flag=true;
temp=ans[cnt-]+str[i];
for(j=;j*j<=temp;j++)
{
if(temp%j==)
{
flag=false;
break;
}
}
if(flag)
{
ans[cnt++]=str[i];
str[i]=;
dfs(step+);
str[i]=ans[--cnt];
ans[cnt]=;
}
}
}
}
} int main()
{
int count=;
while(scanf("%d",&n)!=EOF)
{
cnt=;
printf("Case %d:\n",count++);
dfs();
puts("");
}
return ;
}

HDUOJ----(1016)Prime Ring Problem的更多相关文章

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

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

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

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

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

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

  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(经典DFS+回溯)

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

  6. 杭电oj 1016 Prime Ring Problem

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

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

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

  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. HDU 1016 Prime Ring Problem (回溯法)

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

  10. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

随机推荐

  1. mac下java的安装和升级以及相关环境设置

    安装:brew cask install java8 如果存在多个java,可以设置JAVA_HOME指定java版本 打开终端,执行/usr/libexec/java_home -V 查看MAC下J ...

  2. DDR SDRAM芯片DQS的作用以及读写DQS/DQ对齐方式不同的原因

    节选内容转载自https://www.design-reuse.com/articles/13805/the-love-hate-relationship-with-ddr-sdram-control ...

  3. 混沌数学之拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)

    拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)是 1979年苏联物理学家拉比诺维奇和法布里康特提出模拟非平衡介 质自激波动的非线性常微分方程组: dot{x ...

  4. DB-library 常用函数

    以下转自:http://blog.csdn.net/lwbeyond/article/details/5620801 1. Dbcmd和dbfcmd 函数原形: Dbcmd(DBPROCESS *pr ...

  5. 第十二章 springboot + mongodb(复杂查询)

    简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(Bas ...

  6. Terrain tessellation &&Threaded Rendering Vk

    https://github.com/NVIDIAGameWorks/GraphicsSamples/tree/master/samples/es3aep-kepler/TerrainTessella ...

  7. 浅谈Jquery中的bind()、live()、delegate()、on()绑定事件方式

    一.on(),live(),bind() on() 为指定的元素,添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数.使用 on() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建 ...

  8. svmtrain princomp 出现的问题

    错误一: >> modelw = svmstrain(wine_label,wine_data); Undefined function 'svmstrain' for input arg ...

  9. php实现支付宝授权登录

    第一步: 登录到蚂蚁金服开放平台https://open.alipay.com/platform/home.htm,前提是有商户号.创建应用之后,然后到开发者中心开通对应功能.如图: 第二步: 到应用 ...

  10. 学习Struts框架系列(三):声明式异常处理

    在Struts1.X的版本中加入了对异常的处理Exception Handler,有了它我们可以不使用try/catch捕获异常,一旦出现了我们已经定义的异常,那么就会转到相应的页面,并且携带异常信息 ...