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. AIX用户管理

    用户和组管理     /etc/passwd     /etc/security/.profile     /etc/security/limits     /etc/security/passwd ...

  2. 【书评】RHCSA/RHCE Red Hat Linux 认证学习指南(第6版)EX200 & EX300

    这次参加 CSDN 举办的读书活动,正赶上项目忙,看得也是断断续续,拖了2周了,才能来写这个书评. ========== 书评的分割线 ========== 首先,我会肯定的告诉你,不论你是一名专业的 ...

  3. hdu 5014 思维题/推理

    http://acm.hdu.edu.cn/showproblem.php?pid=5014 从小数開始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种非常重要的方法啊 ...

  4. JVM学习笔记(一)------的基本结构

    从Java视台的逻辑结构.我们能够从图中学习JVM: 您可以从图表清楚地看到Java该平台包括各种逻辑模块.还可以学习JDK与JRE差异 于JVM它们的物理结构,我们可以看一下从图计划: 为JVM学习 ...

  5. js传真实地址 C:\fakepath

    js给action传真是地址的时候,处于安全,传到action中 浏览器会改变路径变为C:\fakepath\ftp.txt,但是原始路径却是 C:\Documents and Settings\Ad ...

  6. [ExtJS5学习笔记]第第二十四次 Extjs5形式上gridpanel或表单数据后台传输remoteFilter设定

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39667533 官方文档:http://docs.sencha.com/extjs/5. ...

  7. NSHashTable 和 NSMapTable学习

    今天,在实现play gif时间功能,我看见两个陌生班,只需看看这个纪录: NSSet和NSDictionary是两个经常使用的类,可是他们默认假定了当中对象的内存行为.对于NSSet.object是 ...

  8. PAT 1055

    题目链接:https://www.patest.cn/contests/pat-b-practise/1055 分析:思路很巧妙,感觉很有意义的字符串题目 #include<bits/stdc+ ...

  9. C/C++头文件

    C/C++头文件 #include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include <errno.h> ...

  10. 慕尼黑大学公开课 Competitive Strategy(竞争策略)总结

    第一章博弈 同时的博弈:双方同时定制策略 如果有显著的次优策略总是不如另一个,则剔除它. 如果一个策略组合中没有一方可以单独改变其策略以提高回报,则称为Nash均衡.一个游戏可能没有也可能有多个Nas ...