Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42688    Accepted Submission(s): 18919

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
 
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
int n,buf[MAXN],vis[MAXN];
bool isPrime(int x)
{
if(x<=) return false;
for(int i=;i*i<=x;i++)
{
if(x%i==) return false;
}
return true;
}
void dfs(int dep)
{
if(dep==n)
{
for(int i=;i<dep-;i++)
{
printf("%d ",buf[i]);
}
printf("%d\n",buf[dep-]);
return ;
}
for(int i=;i<=n;i++)
{
if(!vis[i]&&isPrime(i+buf[dep-]))
{
if(dep==n-&&!isPrime(i+buf[]))
continue;
vis[i]=;
buf[dep]=i;
dfs(dep+);
vis[i]=;
}
}
}
int main()
{
int cas=;
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
printf("Case %d:\n",++cas);
buf[]=;
dfs();
printf("\n");
}
return ;
}

HDOJ1016(标准dfs)的更多相关文章

  1. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  2. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  3. HDU 1983 Kaitou Kid - The Phantom Thief (2)

    神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...

  4. 【BZOJ2019】nim

    直播写题这刺激233 原题: 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输. ...

  5. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  6. hdoj1016 [dfs]

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意: 已知一个数n,在1-n(包含 n ,0 < n < 20)里组合形成一个环形,使得每两 ...

  7. hdoj--1016<dfs>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述:1-n的整数排成一个环,首尾相连,相邻的两个数相加是素数,输出满足的排列,1开头输出, ...

  8. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  9. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

随机推荐

  1. HDU 4783 Clumsy Algorithm

    题意不提. 我们可以发现,可以将最终序列分为对于第i个位置i-pi>=0与i-pi<0种两个子序列.且如果f[n]==g[n],则有两个子序列都递增. 原因是f[n]表示1-n这个排列的逆 ...

  2. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  3. linux安装Zabbix监控

    源码包3.4.0下载  https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.0/zabbix-3.4 ...

  4. 转:gdb相关学习

    声明本文转自:http://www.cnblogs.com/rosesmall/archive/2012/04/12/2444431.html (查看内存部分有补充) 另推荐一个入门级的好文章:htt ...

  5. R语言笔记001——读取csv格式数据

    读取csv格式数据 数据来源是西南财经大学 司亚卿 老师的课程作业 方法一:read.csv()函数 file.choose() read.csv("C:\\Users\\Administr ...

  6. 执行安装redis报错undefined reference to `__sync_add_and_fetch_4'

    执行make命令时报错:   zmalloc.o: In function `zmalloc_used_memory': /var/lib/tcommsvr/redis-2.8.0-rc4/src/z ...

  7. andriod sdk 模拟器的安装以及root

    安卓模拟器常用的一共2种java sdk 和 bluestacks 蓝手指.前者比较卡,后者貌似不能模拟2g/3g上网.   本文主要是针对java sdk的安装使用,以及root.关于root的意思 ...

  8. JavaScript 编程模式

    编程模式,是源自经验和探索总结出的最佳实践方案,既有助于可读性和可维护性,也有助于提升整体性能. 行为隔离 总则:结构.样式和行为之间两两隔离. 避免在结构中使用内联事件 尽量少用 <scrip ...

  9. 机器学习(三)—线性回归、逻辑回归、Softmax回归 的区别

    1.什么是回归?  是一种监督学习方式,用于预测输入变量和输出变量之间的关系,等价于函数拟合,选择一条函数曲线使其更好的拟合已知数据且更好的预测未知数据. 2.线性回归  于一个一般的线性模型而言,其 ...

  10. 删除rime输入法

    mac: 首先将输入法从偏好设置-键盘-输入源中去除,添加系统的输入法. 然后执行命令: $ killall Squirrel $ sudo rm -Rf "/Library/Input M ...