Prime Ring Problem

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

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的组合,使得相邻两个数之和为素数;
分析:预处理40之间的素数,然后回溯;
 #include<iostream>
#include<cstring>
#define N 25
#define M 40
using namespace std; bool is_prime[M],visited[N];
int n,test,ans[N]; void work(int k)
{
int i;
if(k==n+)
{
if(!is_prime[ans[n]+ans[]]) return ;
for(i=;i<=n-;i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
return ;
}
for(i=;i<=n;i++)
{
if(!visited[i]&&is_prime[ans[k-]+i])
{
visited[i]=true;
ans[k]=i;
work(k+);
visited[i]=false;
}
}
} bool prime(int n)
{
if(n==) return false;
if(n==||n==) return true;
int i;
for(i=;i<n;i++)
if(n%i==)
return false;
return true;
} int main()
{
int i;test=;
for(i=;i<M;i++) is_prime[i]=prime(i);
while(cin>>n)
{
ans[]=;
memset(visited,false,sizeof(visited));
cout<<"Case "<<test<<":"<<endl;
work();
test++;
cout<<endl;
}
return ;
}

hdu 1016 Prime Ring Problem(深度优先搜索)的更多相关文章

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

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

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

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

  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. HDU 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 (素数环经典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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  9. hdu 1016 Prime Ring Problem(dfs)

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

随机推荐

  1. python urllib2 Basic认证

    1.通过添加http header 来实现 import urllib2 from base64 import encodestring url = 'http://202.108.1.51' use ...

  2. set ver on/off

    set   verify(或ver)   on/off可以设置是否显示替代变量被替代前后的语句 SQL> set verify on SQL> select &num from d ...

  3. Unplugging一个PDB

    Unplugging一个PDB Unplugging一个pdb不等于remove一个pdb Unplugging一个pdb会创建一个对应的xml文件,借助该xml文件可以将其添加到其他的cdb pdb ...

  4. linux:centos准备及安装

    1>.安装前准备(将虚拟机和映像文件iso下载好) 1.1>.centos下载(建议使用Filezilla下载(http://filezilla-project.org/download. ...

  5. tomcat部署方法总结

    可以参考之前的:http://www.cnblogs.com/youxin/archive/2013/01/18/2865814.html 在Tomcat中部署Java Web应用程序有两种方式:静态 ...

  6. Codeforce Round #214 Div2

    我是不是快要滚蛋了,这次CF爆0? 居然第一题都过不去了,妈蛋附近有没有神经病医院,我要去看看! 精力憔悴! 第一题,我以为要恰好这么多钱,不能多余,想想这也没必要,不符合逻辑,及自己就是这么傻逼! ...

  7. poj: 1004

    简单题 #include <iostream> #include <stdio.h> #include <string.h> #include <stack& ...

  8. 五种常见的电子商务模式对比:B2B、B2C、C2B、C2C、O2O

    电子商务模式是指企业运用互联网开展经营取得营业收入的基本方式,也就是指在网络环境中基于一定技术基础的商务运作方式和盈利模式.目前,常见的电子商务模式主要有B2B.B2C.C2B.C2C.O2O等几种, ...

  9. yii框架中邮箱激活(数字签名)

    控制器: //发送邮箱,激活账号   public function actionEmail()   {        $email=Yii::$app->request->get('em ...

  10. 利用API自动建立GL科目段组合

    1.检查存在性,如没有则新增 fnd_flex_keyval.validate_segs('CREATE_COMBINATION'                                    ...