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. 语音信号处理之(三)矢量量化(Vector Quantization)

    语音信号处理之(三)矢量量化(Vector Quantization) zouxy09@qq.com http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门 ...

  2. VPS服务器下的centos网卡配置详解……

    自动激活网卡 安装了CENTOS 6.X后,每次启动了系统都需要手动激话网卡,以下方法可以在系统启动后自动激活网卡. cat /etc/sysconfig/network-scripts/ifcfg- ...

  3. 计算机内存碎片(中)——外部碎片化(内存 & 文件系统 & 数据库系统通杀)

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/17252221 外部碎片化 当计算机内存被划分成很细碎 ...

  4. Windows的自带控件(比如TButton)大多数消息都由它自己处理,Delphi覆盖了那么多WM_函数优先级较低,一般用不上

    在空窗体上放一个TButton,一个TPanel,然后把在TWinControl.WMEraseBkgnd里下断点: procedure TWinControl.WMEraseBkgnd(var Me ...

  5. vc 制作图片资源dll

    方法一: 使用纯WIN32 DLL方法封装纯资源第一步,通过VS2005建立WIN32 DLL 空工程第二步,设置配置属性->链接器->高级->无入口点(是/NOENTRY)设置配置 ...

  6. 使用ThinkPHP+Uploadify实现图片上传功能

    首先,将下载的Uploadify压缩包解压放到公共文件夹内.实现代码如下: 前台html部分: <script src="/uploadify/jquery.min.js" ...

  7. Mysql RR隔离更新列没有索引 会锁全表

    <pre name="code" class="html">mysql> show variables like '%tx_isolation ...

  8. Android入门第六篇之ListView (一)

    本文来自http://blog.csdn.net/hellogv/ ListView是一个经经常使用到的控件,ListView里面的每一个子项Item能够使一个字符串,也能够是一个组合控件.先说说Li ...

  9. hdu 2594 Simpsons’ Hidden Talents 【KMP】

    题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...

  10. OpenCV-Python教程(4、形态学处理)

    提示: 转载请详细注明原作者及出处,谢谢! 本文介绍使用OpenCV-Python进行形态学处理 本文不介绍形态学处理的基本概念,所以读者需要预先对其有一定的了解. 定义结构元素 形态学处理的核心就是 ...