传送门:

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. Spring学习笔记:jdbcTemplate和数据源配置

    一.使用Spring框架jdbcTemplate实现数据库的增删改查 1.数据库 /* SQLyog Ultimate v8.32 MySQL - 5.7.19-log : Database - in ...

  2. spring历史和哲学

    spring 历史: 2004年 Spring Framework 1.0 final 正式问世. 1.在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分 ...

  3. MySQL:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    错误 原因:可能是服务没有启动 以管理员身份打开cmd 输入 net start mysql

  4. 怎样关闭占用80端口的pid为4的进程

    我也被这个问题给纠结了好几天.重装系统都三次了.终于找到原因了:我用的是sqlserver 2008;解决方法:window-sqlserver 2008-配置工具-sqlserver 配置管理器 找 ...

  5. 一个对inner jion ...on 的sql多表联合查询的练习

    create database practiceSql; use practiceSql; -- create table student( `id` bigint not null auto_inc ...

  6. autocomplete 属性 清除input框输入存留历史值,防止下拉历史值显示

    autocomplete 属性规定输入字段是否应该启用自动完成功能. 自动完成允许浏览器预测对字段的输入.当用户在字段开始键入时,浏览器基于之前键入过的值,应该显示出在字段中填写的选项. 注释:aut ...

  7. avalon2简单数据绑定(自定义属性绑定)

    <!DOCTYPE html> <html> <head> <title>项目</title> <meta charset=" ...

  8. CentOS 7运维管理笔记(11)----PHP安装与配置

    PHP的安装同样需要经过环境检查.编译和安装3个步骤. 1.首先用百度搜索 “PHP:Downloads”, 点击第一个网页: 选择5.5.37版本,选择 .tar.gz 格式的文件: 来到镜像列表网 ...

  9. 六、使用media实现响应式布局

    常见写法: 下面总结常见的响应式布局的分类: @media screen and (max-width:320px){ #talkFooter .editArea{…… } } @media scre ...

  10. 斐波那契数列(C++ 和 Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 1. 写一个函数,输入 n, 求斐波那契(Fibonacci ...