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

本题就是考递归搜索的能力。

数据不大。其它Prime, map等的优化都没多大作用的。

记得记录好数据,就不会有问题了。

只是HDU的推断系统的确垃圾,其它OJ都不会在意末尾多一个空格或者回车的问题的,HDU就一个空格一个回车都一定要依照她的格式。否则就presentation error.

#include <stdio.h>
const int MAX_N = 20;
int num;
int cycle[MAX_N];
bool vis[MAX_N]; bool isPrime(int n)
{
for (int i = 2; i*i <= n; i++)
if (n % i == 0) return false;
return true;
} bool isLegal(int val, int i)
{
int left = i-1;
if (!isPrime(val+cycle[left])) return false;
if (i+1 == num && !isPrime(val+cycle[0])) return false;
return true;
} void printNums(int i = 1)
{
if (i == num)
{
for (int j = 0; j+1 < num; j++)
{
printf("%d ", cycle[j]);
}
printf("%d\n", cycle[i-1]);
return ;
}
for (int v = 2; v <= num; v++)
{
if (vis[v]) continue;
if (isLegal(v, i))
{
cycle[i] = v;
vis[v] = true;
printNums(i+1);
vis[v] = false;
}
}
} int main()
{
int t = 0;
cycle[0] = 1;
while (scanf("%d", &num) != EOF)
{
printf("Case %d:\n", ++t);
for (int i = 2; i <= num; i++) vis[i] = false;
printNums();
putchar('\n');
}
return 0;
}

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

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

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

  2. [HDU 1016]--Prime Ring Problem(回溯)

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

  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(经典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(深度优先搜索)

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

  7. HDU 1016 Prime Ring Problem (DFS)

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

  8. HDU 1016 Prime Ring Problem (回溯法)

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

  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. javascript 判断IOS版本号

    先来观察 iOS 的 User-Agent 串: iPhone 4.3.2 系统: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; ...

  2. OpenCV中遇到Microsoft C++ 异常 cv::Exception

    我在实现<OpenCV2计算机视觉编程手册>第2章 2.2 节 存取像素值 中的椒盐噪声例子中遇到的程序错误. 原始输入程序: #include <opencv2/core/core ...

  3. Kendo UI开发教程(25): 单页面应用(三) View

    View为屏幕上某个可视部分,可以处理用户事件. View可以通过HTML创建或是通过script元素.缺省情况下View将其所包含的内容封装在一个Div元素中.Kendo创建View有两种方式: 使 ...

  4. 让Delphi XE2程序支持UAC

    在win7下,开发的程序有的时候莫名其妙就不能正常工作了,其实都是因为权限不够,要想能够正常运行,就需要获得管理员权限,这就需要处理UAC.具体方法如下: 一,制作“uac.manifest”文件.新 ...

  5. 使用commons-daemon启动、关闭java程序

    系统环境: CentOS 7 X64 JDK1.8 一: 安装jsvc 下载 commons-daemon的源代码包 http://apache.fayea.com//commons/daemon/s ...

  6. 四个机器学习一步一步入门约束波尔兹曼机RBM

  7. javascript(七)document.write

    <h1>test</h1> <button type="button" onclick="my_function">点击me ...

  8. 安装logstash,elasticsearch,kibana三件套(转)

    logstash,elasticsearch,kibana三件套 elk是指logstash,elasticsearch,kibana三件套,这三件套可以组成日志分析和监控工具 注意: 关于安装文档, ...

  9. 实现web多语言的一种解决办法

    实现web多语言可能有多种解决办法,现在分享一种比较简单的思路,这篇文章主要用于记录学习过程,肯定存在不少谬误,欢迎批评指正. web多语言实现最简单的一种方法可能是每一种语言一套代码,但这样存在一个 ...

  10. 14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量

    14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量 InnoDB 使用bac ...