题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

Prime Ring Problem

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

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

 
Source
 简单的dfs
直接上代码:
这里介绍一个报错:Floating point exception (core dumped) linux下报这个一般就是出现了除0或者模0操作....写代码要仔细呀
 #include<cstdio>
#include<cstring>
using namespace std;
#define N 50
int a[N];
bool vis[N];
bool is_prime[N];
void init()
{
for(int i = ;i < N ;i++) is_prime[i] = ;
for(int i = ;i < N ;i++)
{
for(int j = ; j <= i/ ;j++)
{
if(i%j==) {is_prime[i] = ; continue;}
}
}
}
void dfs(int n, int cnt)
{
if(cnt == n&&is_prime[a[]+a[n-]])
{
for(int i = ; i < n- ;i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n-]);
} for(int i = ;i <= n ;i++)
{
if(!vis[i]&&is_prime[a[cnt-]+i])
{
a[cnt] = i;
vis[i] = ;
dfs(n,cnt+);
vis[i] = ;
}
}
} int main()
{
int n;
int c = ;
init();
while(~scanf("%d",&n))
{
printf("Case %d:\n",++c);
a[] = ;
memset(vis,,sizeof(vis));
vis[] = ;
dfs(n,);
puts("");
}
return ;
}

Prime Ring Problem(dfs水)的更多相关文章

  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 (DFS)

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

  3. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  4. Prime Ring Problem (DFS练习题)

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

  5. hdu1016 Prime Ring Problem(DFS)

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

  6. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  7. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  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. HDU1016 Prime Ring Problem(DFS回溯)

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

随机推荐

  1. Python学习日记:day5-------dict字典

    #字典dict------->唯一的映射类型 1.数据类型的划分 数据类型划分为可变数据类型和不可变数据类型. 不可变数据类型:tupe(元组).bool.int.str           可 ...

  2. JVM类加载机制以及类缓存问题的处理

    一:JVM类加载机制 和 类缓存问题的处理 当一个java项目启动的时候,JVM会找到main方法,根据对象之间的调用来对class文件和所引用的jar包中的class文件进行加载(其步骤分为加载.验 ...

  3. 【quickhybrid】iOS端的项目实现

    前言 18年元旦三天内和朋友突击了下,勉强是将雏形做出来了,后续的API慢慢完善.(当然了,主力还是那个朋友,本人只是初涉iOS,勉强能看懂,修修改改而已) 大致内容如下: JSBridge核心交互部 ...

  4. [编织消息框架][网络IO模型]NIO(select and poll)

    上面测试论证系统内核在read data时会阻塞,如果我们在把第一个阶段解决掉那么性能就会提高 NIO 编程 JDK 1.4中的java.nio.*包中引入新的Java I/O库,其目的是提高速度.实 ...

  5. [转]Linux网络配置命令ifconfig输出信息解析

    eth0      Link encap:Ethernet  HWaddr 00:1e:4f:e9:c2:84 inet addr:128.224.163.153  Bcast:128.224.163 ...

  6. volatile关键字的特性总结

    当一个变量定义为volatile后,它将具备两个特性: 1.保证此变量对所有线程的可见性,所谓"可见性",,是指当一个线程修改了这个变量的值,新值对于其他线程来说是可以立即得知的. ...

  7. Fiddler的script用法

    一.关于Fiddler Script Fiddler Script 是用JScript.NET语言写的.Fiddler 包含了一个脚本文件可以自动修改Http Request 和Response.这样 ...

  8. VS2015 查看类之间的继承关系

    ---恢复内容开始--- 1. 右击项目名称,单击"查看"菜单下的"查看类图"菜单: 2.生成的类图如下:

  9. Upgrade with the Gradle Wrapper, gradlew升级

    springboot 2.0需要gradle 1+, 而自动构建的都是3.+,手动升级如下 Upgrade with the Gradle Wrapper If your existing Gradl ...

  10. (2环境架设)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练

    从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...