算法:搜索

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

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++中的拷贝构造函数和赋值构造函数

    代码: #include <iostream> #include <cstdio> using namespace std; class A{ public: A(){ cou ...

  2. 公众号的秘密,知道一个biz就够了

    公众号的秘密,知道一个biz就够了 微信对于我来说,最有价值的是一个学习渠道,特别是搜狗微信搜索(http://weixin.sogou.com/)能够很方便的搜索公众账号和文章内容,PC端就能够获得 ...

  3. Extjs 兼容IE10

    在对应的地方将Ext.isIE 修改成: Ext.isIE && !(/msie 9/.test(navigator.userAgent.toLowerCase())  &&a ...

  4. JDK + Tomcat 安装配置

    学习Java 开发的第一步就是配置环境,今天第一次配置,把过程记录下以备后用. 一.下载JDK.Tomcat JDK:http://www.oracle.com/technetwork/java/ja ...

  5. AngularJS指令的作用域

    参考文章:https://segmentfault.com/a/1190000002773689

  6. vmware安装Linux时无法打开xpdf

    vmware10+redhat9 在装第二张镜像文件时,出现如下提示:无法打开xpdf-2.01-8软件包...... 解决方法: vmware中,虚拟机->设置->硬件->CD/D ...

  7. python 微信推送模板消息

    #!/usr/bin/env python #-*- coding: utf-8 -*- import httplib import json import MySQLdb #从数据库中获取acces ...

  8. Activiti工作流学习-----基于5.19.0版本(4)

    四.使用工作流开发 org.activiti.engine.ProcessEngine提供的Service作用在工作流引擎上面,如果所示是模仿一个公司简单的审批流程,你可以下载这个Demo:Activ ...

  9. UART与USART的区别

    UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...

  10. 自制单片机之十七……PC与单片机RS-232串口的通讯和控制

    这次我们来试着一步步的去掌握PC与单片机通过RS-232进行通讯和控制. 先说说我硬件的情况.我用的PC是个二手的IBM240小本本,十寸屏,赛扬400,机子很老了.但也有它的优点:1.串口,并口,P ...