Prime Ring Problem

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

Total Submission(s): 25156    Accepted Submission(s): 11234

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
</pre><pre name="code" class="cpp">#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; int date[25]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int ans[25];
int n, cnt;
int flag; void dfs(int step)
{
int temp;
if(step==n) //深搜究竟时注意推断首尾是否符合要求
{
flag=1;
temp = ans[0]+ans[n-1];
if(flag)
{
for(int i=2; i*i<=temp; i++)
if(temp%i==0)
{
flag=0;
break;
}
}
if(flag)
{
printf("%d", ans[0]);
for(int i=1; i<n; i++)
printf(" %d", ans[i]);
printf("\n");
}
}
else
{
for(int i=1; i<n; i++)
{
if(date[i])
{
temp=ans[cnt-1]+date[i];
flag=1;
for(int j=2; j*j<=temp; j++)
{
if(temp%j==0)
{
flag=0;
break;
}
}
if(flag)
{
ans[cnt++]=date[i];
date[i]=0;
dfs(step+1);
date[i]=ans[--cnt]; //记得将数据的还原处理
ans[cnt]=0;
}
}
}
}
} int main()
{
int count=1;
while(scanf("%d", &n)!=EOF)
{
memset(ans, 0, sizeof(ans));
cnt=1;
ans[0]=1;
printf("Case %d:\n",count++);
dfs(1); //从1開始进入搜索
printf("\n"); //每次输出后记得加个换行
} return 0;
}

HDU 1016:Prime Ring Problem的更多相关文章

  1. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. 九度oj 题目1459:Prime ring problem

    题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...

  3. DFS:Prime Ring Problem(素数环)

    解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...

  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(回溯)

    题目链接: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. HDOJ(HDU).1016 Prime Ring Problem (DFS)

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

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

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

随机推荐

  1. 小米手机不能直接运行Android Studio程序

    小米手机不能直接运行Android Studio程序 转载自:http://www.jianshu.com/p/6588c69b42cf Problem description: Android St ...

  2. 【堆栈平衡的说明太有才了】转贴自Jim&#39;s blog

    先说明.原发者iso9001 http://www.ghoffice.com/bbs/read.php?tid-35165.html他提供的地址(当他是个指针好了:P)http://ajiannet. ...

  3. 宿主机訪问virtualBox中Ubuntu

    斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz).转载请注明出处! 使用NAT模式.就是让虚拟系统借助NAT(网络地址转换)功能.不须要你进行不论什么其它的 ...

  4. django:MySQL Strict Mode is not set for database connection 'default'

    ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'        HINT: MySQL's ...

  5. python with和上下文管理工具

    对于系统资源如文件.数据库连接.socket 而言,应用程序打开这些资源并执行完业务逻辑之后,必须做的一件事就是要关闭(断开)该资源. 比如 Python 程序打开一个文件,往文件中写内容,写完之后, ...

  6. servlet中获得tomcat项目根目录的绝对路径

    public class CreateXmlAction extends HttpServlet { private ServletConfig config; public void init(Se ...

  7. Node.js:Express 框架

    Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具.使用 Express 可以快速地搭建一个完整功能的网站 ...

  8. An extensible Factory Pattern example

    I have read the book Api Design For C++ recently , and I must say that it is a masterpiece. There is ...

  9. ASP.NET缺少程序集引用怎么办

    如下所示,提示缺少Windows的引用,在using System.Windows.Forms;有一个下划线 错误代码为: 错误    1    命名空间"System"中不存在类 ...

  10. 关于angularjs中路由页面强制更新的问题

    有这么一个问题,在页面内路由页面跳转时,第一次跳入路由页面时是正常的,但是第二次会记住第一次时的状态,有时候并不想这样,想强制更新这个路由页面. 有一种方式就是使用 ui-sref-opts功能,我试 ...