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 <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;

int n;
int vis[25];
int prime[38]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1};
int ans[25];

void print(int m)
{
int i;
printf("%d",ans[1]);
for(i=2;i<=n;i++)
{
printf(" %d",ans[i]);
}
printf("\n");
}

void dfs(int point)
{
if(point==n && prime[ans[point]+ans[1]])
{
print(point);
}
else
{
for(int j=2;j<=n;j++)
{
if(!vis[j]&& prime[ans[point]+j])
{
ans[point+1]=j;
vis[j]=1;
dfs(point+1);
vis[j]=0;
}
}
}
}

int main()
{
int cas=0;
while(scanf("%d",&n)!=EOF)
{
cas++;
memset(vis,0,sizeof(vis));
vis[1]=1;
ans[1]=1;
printf("Case %d:\n",cas);
dfs(1);
printf("\n");
}
}

 

Prime Ring Problem的更多相关文章

  1. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  2. hdu 1016 Prime Ring Problem(DFS)

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

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  4. 杭电oj 1016 Prime Ring Problem

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

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

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

  6. HDU1016 Prime Ring Problem(DFS回溯)

    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. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  9. HDU 1016 Prime Ring Problem (回溯法)

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

  10. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

随机推荐

  1. for语句嵌套循坏性能的剖析

    日常工作中,处理数据难免会遇到遍历,for循环可能是我们用的比较多的了.本节就来探讨下for语句嵌套循环的性能,猜想下面两个语句的性能. 语句1 ; i < ; i++){ ; j < ; ...

  2. Sql Server如何新建计划任务

    打开Sql Server数据库,在管理中找到维护计划功能,右击维护计划选择维护计划向导.如图所示: 打开维护计划向导后,在第一个步骤下输入计划名称. 输入完计划名称后,点击右下角的更改按钮,这里是配置 ...

  3. web.config 修改数据库连接

    <connectionstrings> </database=数据库名字 ;uid = 登录数据库的名字;Password = 登录数据库的密码;/> </connect ...

  4. [MISSAJJ原创]cell内 通过SDWebImage自定义创建动态菊花加载指示器

    最后更新已经放到了github上了 MISSAJJ自己写的一个基于SDWebImage自定义的管理网络图片加载的工具类(普通图片加载,渐现Alpha图片加载,菊花Indicator动画加载) 经常在项 ...

  5. Intellij Idea 编辑器使用之 安装、破解 版本15.0.1

    工欲善其事必先利其器. 早就听说了Intellij idea 这个编辑器.也曾尝试过,由于对eclipse,myeclipse用的比较习惯.顺手了,所以对于Intellij idea 也只是浅尝辄止. ...

  6. .net中的序列化

    常见的序列化格式和方法 在.net中,常见的序列化格式主要有json,二进制和xml,总结如下表格. 注意事项 关于实体特性标注规则: 1,.net中所有用于序列化的实体的class上应该加上[Ser ...

  7. WCF初探文章列表

    WCF初探-1:认识WCF WCF初探-6:WCF服务配置 WCF初探-2:手动实现WCF程序 WCF初探-7:WCF服务配置工具使用 WCF初探-3:WCF消息交换模式之单向模式 WCF初探-8:W ...

  8. 在CentOS 6.7中安装 latex2html, 实现 latex 自动转化为 word

    由于latex文档生成的pdf不便于提供审阅和修改,亟需一个软件实现从latex向word文档的自动转换( 如何把Latex自动转为Word 介绍了由latex2html 可实现这个功能,这也是初衷) ...

  9. AutoCAD学习笔记

    学习笔记: **有些命令,有两到三种执行方式:菜单.命令行.对话框.如layer命令,如果在命令行打入layer命令,就会弹出对话框主y式,如果要命令行方式执行,就需要在前面加一个-号,即-layer ...

  10. Ubuntu导入证书

    (*.cer) 首先我们得进入JAVA_HOME目录(查看/etc/profile文件),然后进去以下路径/opt/jdk1.5.0_11/jre/lib/security/ 将证书放进去,假如说是1 ...