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. 关于Mysql几周的整理文档

    https://files.cnblogs.com/files/swobble/mysql.rar 内容包括 版本测试(5.5,5.6,5.7) 平台测试(windows所有平台) 文件说明 精简说明 ...

  2. 廖雪峰Java13网络编程-1Socket编程-1网络编程概念

    1.计算机网络 1.1 什么是计算机网络? 两台或更多计算机组成的网络 同一网络内的任意2台计算机都可以直接通信 所有计算机必须遵循同一种网络协议 1.2 什么是互联网 互联网是网络的网络 互联网采用 ...

  3. thinkphp Mongo模型

    Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...

  4. python相关软件安装流程图解————————pycharm安装——————pycharm-professional-2018.3.1

    https://www.jetbrains.com/pycharm/download/#section=windows http://www.cnblogs.com/ceshi2016/p/91129 ...

  5. CSS自动换行、强制不换行、强制断行、超出显示省略号

    转自:https://blog.csdn.net/liuyan19891230/article/details/50969393 P标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是 ...

  6. python 编程 一次错误记录 -1073740791

    原因是发生在我错把类当做实例化对象使用了

  7. Consul 集群搭建

    搭建集群:.启动node1机器上的Consul (node1机器上执行) consul agent -data-dir /tmp/node1 -node=node1 -bind=192.168.0.1 ...

  8. SpringCloud学习笔记(一):SpringCloudt相关面试题

    什么是微服务? 看笔记二 微服务之间是如何独立通讯的? 服务与服务间采⽤轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API) SpringCloud和Dubbo有什么区别? Du ...

  9. 组件component

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  10. 04-4-typeof

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...