传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1016

Prime Ring Problem

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

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
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1312 1072 1242 1175 1253 
 
分析:
经典的素数环问题
dfs
就是全排列的基础上加上素数环要求的检测
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define max_v 105
int a[max_v];
int vis[max_v];
int n;
int isp(int x)//素数检测
{
for(int i=;i<=sqrt(x);i++)
{
if(x%i==)
return ;
}
return ;
}
void dfs(int cur)//素数环问题 全排列思想加素数的检测
{
if(cur==n&&isp(a[]+a[n-]))//判断到最后一个数了
{
printf("%d",a[]);//打印
for(int i=;i<n;i++)
{
printf(" %d",a[i]);
}
printf("\n");
return ;
}else
{
for(int i=;i<=n;i++)//找适合放在cur位置的i
{
if(!vis[i]&&isp(i+a[cur-]))//满足要求
{
a[cur]=i;//放入
vis[i]=;//标记
dfs(cur+);//搜索
vis[i]=;//回退
}
}
}
}
int main()
{
int t=,k=;
while(~scanf("%d",&n))
{
//if(k)
//printf("\n");
memset(vis,,sizeof(vis));//标记清空
a[]=;//确定1的位置
printf("Case %d:\n",t);
dfs();//从1开始放数
t++;
k++;
printf("\n");
}
return ;
}
 

HDU 1016 Prime Ring Problem(素数环问题)的更多相关文章

  1. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  2. HDOJ 1016 Prime Ring Problem素数环【深搜】

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

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

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

  4. [HDU 1016]--Prime Ring Problem(回溯)

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

  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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  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. zookeeper【4】master选举

    考虑7*24小时向外提供服务的系统,不能有单点故障,于是我们使用集群,采用的是Master+Slave.集群中有一台主机和多台备机,由主机向外提 供服务,备机监听主机状态,一旦主机宕机,备机必需迅速接 ...

  2. poj 1155 树形背包

    http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...

  3. apache和tomcat搭建集群

    最近在学习简单的apache服务器和两个tomcat一起搭建集群,这里简单记录一下 1.准备工作 ①搭建一个可以运行的web项目 用maven搭建springmvc项目 ,只要将这里面的web.xml ...

  4. Web前端面试指导(十一):样式导入有哪些方式?

    样式导入方式 link import 使用方式 link的使用 <link href="index.css" rel="stylesheet"> i ...

  5. js获取当前日期和时间的代码

    最佳答案 var myDate = new Date(); myDate.toLocaleDateString(): //获取当前日期myDate.toLocaleTimeString(); //获取 ...

  6. MySQL--主从数据库同步原理

    主从数据库的复制原理:摘自MySQL官网 1. 异步 * 主在执行sql之后,记录二进制bin-log文件.   * 同时从连接主服务器,并从主获取binlog,存于本地relay-log,并从上次记 ...

  7. KMP算法的一个简单实现

    今天学习KMP算法,参考网上内容,实现算法,摘录网页内容并记录自己的实现如下: 原文出处: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93M ...

  8. C语言string.h中常用字符函数介绍

    原文:http://www.cnblogs.com/xuwenmin888/archive/2013/05/03/3057883.html strcpy 函数名: strcpy 功 能: 拷贝一个字符 ...

  9. k 近邻算法(k-Nearest Neighbor,简称kNN)

    预约助教问题: 1.计算1-NN,k-nn和linear regression这三个算法训练和查询的时间复杂度和空间复杂度? 一. WHy 最简单最初级的分类器是将全部的训练数据所对应的类别都记录下来 ...

  10. as3.2版本中中jar生成方法

    lintOptions { abortOnError false } task makeJar(type: Copy) { //删除存在的 delete 'build/libs/myjar.jar' ...