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 思路:DFS遍历所有结果
AC Code:
#include<iostream>
#include<cstring>
using namespace std;
int num[];
int vis[];
int n;
bool isPrime (int k) {
for (int i = ; i * i <= k; i++)
if (k % i == )
return false;
return true;
}
void dfs (int k) {
if (k > n) {
if (isPrime (num[] + num[n])) {
for (int i = ; i < n; i++)
printf("%d ", num[i]);
printf("%d\n", num[n]);
}
return ;
}
for (int i = ; i <= n; i++) {
if (vis[i] == false) {
if (isPrime (num[k - ] + i)) {
num[k] = i;
vis[i] = true;
dfs (k + );
vis[i] = false;
}
}
}
return ;
}
int main() {
int sum = ;
while(~scanf ("%d", &n)){
printf ("Case %d:\n", sum++);
memset (vis, false, sizeof(vis));
num[] = ;
vis[] = true;
dfs();
putchar('\n');
}
}

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

  1. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

  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. [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(素数环问题)

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

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

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

  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. 差分数组|小a的轰炸游戏-牛客317E

    小a的轰炸游戏 题目链接:https://ac.nowcoder.com/acm/contest/317/E 思路  这题考查的是对差分数组原理和前缀和的理解. 四个数组分别记录朝着四个方向下放的个数 ...

  2. 【修改缓存路径】修改Gradle缓存路径的几种方式

    起因 Android Studio的gradle在缓存处理上有时候会莫名其妙的出问题,必要时需要手动删除缓存,然后重新编译.有时也有出于其他考虑指定gradle缓存路径. 方法1:修改gradle文件 ...

  3. (转载)用C#实现MySQL建库及建表

    最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...

  4. MPI之聚合通信-Scatter,Gather,Allgather

    转自:https://blog.csdn.net/sinat_22336563/article/details/70229243 参考:http://mpitutorial.com/tutorials ...

  5. 配置SSH无密码登录

    首先进入目录 : /home/zuoyan/.ssh 在-的  .ssh 下 使用命令生成密钥 ssh-keygen -t rsa 敲4下回车 然后将公钥配置到需要的机器上,复制的目标机器最后是用户名 ...

  6. MongoDB集群配置笔记二(实战)

    单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...

  7. Hyper-v虚拟机

    Hyper-V1:创建和管理虚拟机 Hyper-V2:向VM增加虚拟硬盘 Hyper-V3:虚拟机的配置 使用Hyper-V创建虚拟机 Hyper-v 安装CentOS 7 (其他虚拟机一样参考)

  8. spring 配置ibatis和自动分页

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. 正则解析json数据

    http://tool.chinaz.com/regex http://tool.oschina.net/regex/

  10. codeforces 768E Game of Stones

    题目链接:http://codeforces.com/problemset/problem/768/E NIM游戏改版:对于任意一堆,拿掉某个次数最多只能一次. 对于一堆石头数量为$X$.找到一个最小 ...