Prime Ring Problem

HDU - 1016

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
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue> using namespace std; int n;
int a[];
int vis[];
bool mark[]; void init() // 素数筛
{
for(int i = ; i < ; ++i)
mark[i] = true; for(int i = ; i < ; ++i)
{
if(mark[i] == true)
{
for(int j = i*i; j < ; j += i)
{
mark[j] = false;
}
}
}
} // 边枚举边判断,不要最后一次性判断,会超时
void dfs(int step)
{
if(step > )
{
if(mark[a[step-]+a[step-]] == false) // 判断最后两个数
return;
} if(step == n+)
{
if(mark[a[n]+a[]] == false) // 判断最后一个数与第一个数
return;
for(int i = ; i < n; ++i)
printf("%d ", a[i]);
printf("%d\n", a[n]); } for(int i = ; i <= n; ++i)
{
if(!vis[i])
{
a[step] = i;
vis[i] = ;
dfs(step+);
vis[i] = ;
} }
} int main()
{
init();
int cas = ;
a[] = ;
while(scanf("%d", &n) != EOF)
{
printf("Case %d:\n", cas++);
memset(vis, , sizeof(vis));
dfs();
printf("\n");
} return ;
}

Prime Ring Problem HDU - 1016 (dfs)的更多相关文章

  1. Prime Ring Problem HDU - 1016

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  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. 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 (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(回溯)

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

  8. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  9. hdu 1016 Prime Ring Problem(DFS)

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

随机推荐

  1. 2016.10.4初中部下午NOIP普及组比赛总结

    2016.10.4初中部下午NOIP普及组比赛总结 这次的题有些水,只是第四题有点坑. 题目: 比赛:AC+0+AC+50=250 改题:AC+AC+AC+50=350 一.Bill 满地都是水 题目 ...

  2. 如何在 Apache Flink 中使用 Python API?

    本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink PMC,阿里巴巴高级技术专家 孙金城 分享.重点为大家介绍 Flink Python API 的现状及未来规划, ...

  3. JS中的Map和Set

    使用iterable内置的forEach方法 var a = ['A', 'B', 'C']; a.forEach(function (element, index, array) { // elem ...

  4. 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台

    项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...

  5. redhat4.4下安装GMT4.5.11

    GMT是地学界常用的开源软件,不仅是因为其开源的特性,还有着独特的魅力. 所需要的软件如下 安装步骤: 1. Put the soft packages in one folder, i.e. /ho ...

  6. eclipse memory analyzer对系统内存溢出堆文件解析(转)

    本文转之:https://blog.csdn.net/rachel_luo/article/details/8992461 前言 性能分析工具之-- Eclipse Memory Analyzer t ...

  7. [原创]关于时间格式的坑(kk:mm:ss、HH:mm:ss与hh:mm:ss)

    笔者在项目中使用 kk:mm:ss表示24小时制,却发现与所想的不同,特记此坑,提醒众人: kk:mm:ss   24小时制,时间为1:00:00-24:59:59 HH:mm:ss 24小时制,时间 ...

  8. linux下mysql导入导出sql文件

    使用mysqldump导出数据库: # mysqldump -u root -p gzy > gzy.sql # mysqldump -u 数据库连接用户名 -p 目标数据库 > 存储的文 ...

  9. linux删除登录日志及历史命令

    参考:https://blog.51cto.com/study2008/2301555 基本常用清理linux日志: # echo > /var/log/wtmp //清除用户登录记录 # ec ...

  10. css 实现单行以及多行文本溢出显示省略号

    如果实现单行文本的溢出显示省略号大家都知道用text-overflow:ellipsis属性来,当然还需要加上宽度width属性. 实现方法: ``` overflow: hidden; text-o ...