算法:搜索

题意:相邻的两个数之和是素数,别忘了最后一个,和第一个

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

代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int a[30],b[45],n,m,c[30];
void cmp()
{ memset(b,1,sizeof(b));
b[1]=b[0]=0;
for(int i=2;i<=22;i++)
{
for(int j=2*i;j<45;j=j+i)
b[j]=0;
}
}
void dfs(int x,int k)
{
if(k==n&&b[c[0]+c[n-1]])
{
for(int i=0;i<n;i++)
{
if(i)cout<<" ";
cout<<c[i];
}
cout<<endl;
}
else
{
for(int i=2;i<=n;i++)
{
if(!a[i]&&b[i+c[k-1]])
{
a[i]=1;
c[k]=i;
dfs(n,k+1);
a[i]=0;
}
}
}
}
int main()
{
int i,j=0;
cmp();
while(cin>>n)
{
for(i=1;i<=n;i++)
a[i]=0;
c[0]=1;
cout<<"Case "<<++j<<":"<<endl;
dfs(n,1);
cout<<endl;
}
return 0;
}

hdu 素数环的更多相关文章

  1. HDU 1016 素数环(dfs + 回溯)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 一道很典型的dfs+回溯: 根据题意首先进行初始化,即第一个位置为1,然后进行dfs, ...

  2. HDU - 1016 Prime Ring Problem 经典素数环

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

  3. nyoj 488 素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  4. nyoj 素数环

    算法:搜索 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输 ...

  5. nyoj素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  6. ny488 素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起 ...

  7. DFS搜索题素数环

    素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...

  8. ACM_素数环(dfs)

    Problem Description: 如图所示,环由n个圆组成. 将自然数1,2,...,n分别放入每个圆中,并且两个相邻圆中的数字总和应为素数. 注意:第一个圆圈的数量应该始终为1. Input ...

  9. EOJ 1114 素数环

    题意 一个由自然数 1…n (n≤18) 素数环就是如下图所示,环上任意两个节点上数值之和为素数. 1 / \   4  2 \ /    3 Input 输入只有一个数 n,表示你需要建立一个 1… ...

随机推荐

  1. [转载]C++ 堆与栈简单的介绍

    在C和C++中,有三种使用存储区的基本方式: [静态存储区(Static   Memory)] 在静态存储区中,连接器(linker)根据程序的需求为对象分配空间.全局变量.静态类成员以及函数中的静态 ...

  2. uva 11536 - Smallest Sub-Array

    题目大意:按照题目中的要求构造出一个序列,找出最短的子序列,包含1~k. 解题思路:先根据题目的方法构造出序列,然后用Towpointer的方法,用v[i]来记录当前[l, r]中有几个i:当r移动时 ...

  3. uva 10107 - What is the Median?

    #include <cstdio> #include <iostream> using namespace std; ]; int main() { int i, cur_in ...

  4. Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试

    在前端开发中,尤其是MVC架构多人开发,负责前端的童鞋总是需要做静态页面,再和后台连接前无法使用变量如EL表达式等测试功能,所以本人引入了一个模板jsp数据测试专用文件,专门配置所有的变量,然后在待测 ...

  5. [Mugeda HTML5技术教程之9]使用元件

    元件是一个可以在舞台上实例化和再利用的预先生成的独立动画.一个元件有它自己的时间轴(层,单位等),可以独立显示的动画.元件提高了动画的重用性和灵活性,是个强大的存在.元件可用于创建复杂的动画效果. 所 ...

  6. ubuntu openssh-server

    1.   sudo apt-get install openssh-server 2.   sudo service ssh start 3.   sudo service ssh status 4. ...

  7. ImageView显示不出来图片

    问题?? 用ImageView控件方法:public void setImageBitmap(Bitmap bm)来显示生成的bimap,结果在图片显示区域显示一片空白,而且其他控件颜色没变化,造成I ...

  8. EBS导出键弹性域

    select gl_flexfields_pkg.get_description_sql(gcc.chart_of_accounts_id, 1, gcc.segment1) || '-' || gl ...

  9. Linux一个简单的读写文件

    (1)linux中的文件描述符fd的合法范围是或者一个正正数,不可能是一个负数. (2)open返回的fd程序必须记录好,以后向这个文件的所有操作都要靠这个fd去对应这个文件,最后关闭文件时也需要fd ...

  10. Android初学:联系创建Activity

    public class Activity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState ...