Prime Ring Problem

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.

 

Inputn (0 < n < 20). 
OutputThe 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组成一个素数环,相邻两个数的和为素数。
首先偶数(2例外,但是本题不会出现两个数的和为2)不是素数,
所以素数环里奇偶间隔。如果n是奇数,必定有两个奇数相邻的情况。
所以当n为奇数时,输出“No Answer”。
当n == 1时只1个数,算作自环,输出1
所有n为偶数的情况都能变成奇偶间隔的环-----所以都有结果。

#include<stdio.h>
#include<string.h>
int n,c=,i;
int a[],b[];
int jo(int a)
{
return a%==?:;
}
int prime[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
void dfs(int step)
{
int i;
if(step>n&&prime[a[]+a[n]]){
for(i=;i<=n;++i){
if(i==) printf("%d",a[i]);
else printf(" %d",a[i]);
}
printf("\n");
return;
}
if(jo(a[step-])){
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
else{
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
}
int main()
{
while(~scanf("%d",&n)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
a[]=;b[]=;
if(n==) printf("Case %d:\n1\n\n",++c);
else if(!jo(n)){
printf("Case %d:\n",++c);
dfs();
printf("\n");
}
}
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(DFS)

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

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

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

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

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

随机推荐

  1. kubernetes高级之创建只读文件系统以及只读asp.net core容器

    系列目录 使用docker创建只读文件系统 容器化部署对应用的运维带来了极大的方便,同时也带来一些新的安全问题需要考虑.比如黑客入侵到容器内,对容器内的系统级别或者应用级别文件进行修改,会造成难以估量 ...

  2. 解析java.math.BigInteger类——构造函数

    最早由于做作业,结识了java的BigInrger类.读着读着,越来越觉得有趣.后来作业做完,也不忍丢下它,索性把全部代码研究一遍. 开始的时候,一个上午时间最多读懂2个方法.但是还是有滋有味的坚持了 ...

  3. 图像处理之滤波---滤波在游戏中的应用boxfilter

    http://www.yxkfw.com/?p=7810 很有意思的全方位滤波应用 https://developer.nvidia.com/sites/default/files/akamai/ga ...

  4. CSS伪类:before 和 :after

    CSS用了许久,对一些伪类熟视无睹,从不想着去搞清楚一下.比如说这个 :before :after 其实,:before 表示该标记前面的样式,反之 :after 代表设置后面的样式.网页上常常看到有 ...

  5. CenterOS下搭建Hadoop环境

    检查防火墙状态 service iptables status 关闭防火墙 service iptables stop 查看防火墙开机启动状态 chkconfig iptables --list 关闭 ...

  6. 九度OJ 1097:取中值 (中值)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5092 解决:1411 题目描述: 存在两组数组,和4个数字a,b,c,d,要求做如下操作,将第一个数组第a个数到第b个数,第二个数组的第c ...

  7. openjdk源码目录结构

    1 openjdk源码 http://hg.openjdk.java.net 选择jdk8u这个project, 然后选择jdk8u20这个repository. 2 目录结构 corba: comm ...

  8. ActiveMQ的消息的(含附件)发送和接收使用

    首先介绍一下ActiveMQ的版本:apache-activemq-5.10.2 启动MQ:activemq.bat 下面来编写MQ的发送类: 里面的发送ip和模式名称可以根据具体的实际情况填写. S ...

  9. 解决表单GET提交后台数据乱码问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ForeverCjl/article/details/36180933     ​在页面上提交数据到s ...

  10. ORACLE函数之单行转换函数

     1           ASCIISTR 格式:ASCIISTR(C) 说明:将字符串C转换为ASCII字符串,即将C中的ASCII字符保留不变,但非ASCII字符则以ASCII表示返回 举例: ...