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. 一些常用的mysql语句实例-以后照写

    create database blog; create table blog_user ( user_Name char(15) not null check(user_Name !=''), us ...

  2. 【做题】CF285E. Positions in Permutations——dp+容斥

    题意:求所有长度为\(n\)的排列\(p\)中,有多少个满足:对于所有\(i \,(1 \leq i \leq n)\),其中恰好有\(k\)个满足\(|p_i - i| = 1\).答案对\(10^ ...

  3. 如何自己烧制全文RSS(打造自己RSS源)

    烧制RSS源 到Feed43注册一个账号,虽说不注册也能用,但是为了方便修改自己烧制的RSS,最好还是注册一个账号来管理 到主页点击Create new feed 输入网址点击reload 可以看到请 ...

  4. lamp服务器被人恶意绑定域名的解决办法

    还没开始就被别人绑定了域名 事情的起因与发现 刚买了个服务器搭建了一个dz,想着域名还没备案,就先搭建了起来,然后在做DDOS测试时偶然发现服务器被别人恶意把域名绑定了 最初的解决方案 没管..... ...

  5. 【2.0新特性】Spring Boot 2.0新特性

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

  6. dh

    -.-- -.. --- -. --- - -.- -. --- .--

  7. 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!

    https://www.cnblogs.com/zhbsh/archive/2013/04/22/3035477.html 用C#.NET调用Java开发的WebService时,先在客户端封装的带有 ...

  8. Sql Ado.net 学习笔记之连接字符串

    https://www.cnblogs.com/heng95/p/5902019.html 连接字符串 SQL Client .net数据提供程序在连接到数据库时极其灵活,它提供了多种用以生成连接字符 ...

  9. RabbitMq的整理 exchange、route、queue关系

    https://blog.csdn.net/samxx8/article/details/47417133

  10. Spring Cloud各组件超时总结

    Ribbon的超时 全局设置: ribbon: ReadTimeout: 60000 ConnectTimeout: 60000 1 2 3 局部设置: service-id: ribbon: Rea ...