题目连接: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学习日记:day9--------函数

    初识函数 1,自定义函数 s ='内容' #自定义函数 def my_len():#自定义函数没有参数 i =0 for k in s: i+=1 print(i) return i #返回值 my_ ...

  2. 《Linux系统编程手册》读书笔记——第2章基本概念

    操作系统的核心--内核 内核的职责 进程调度:Linux属于抢占式多任务操作系统,多个进程可同时驻留于内存,且每个进程都能获得对CPU的使用权.哪些进程获得对CPU的使用,以及每个进程能使用多长时间 ...

  3. bzoj 1486: [HNOI2009]最小圈

    Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 3 Sample Output 3.66666667 HIN ...

  4. Thymeleaf引擎支持Multi Prefix

    最近团队的一个项目在重构,希望引入Thymeleaf减少页面端的代码复杂性.在重构过程中,发现html文件需要保存在多个不同的目录中,但Thymeleaf缺省的实现不支持这种方式. 1        ...

  5. 初学者福音——10个最佳APP开发入门在线学习网站

    根据Payscale的调查显示,现在的APP开发人员的年薪达到:$66,851.这也是为什么那么多初学的开发都想跻身到APP开发这行业的主要原因之一.每当你打开App Store时候,看着琳琅满目的A ...

  6. .net 4.5 webform 提取ModelState错误信息

    .net4.5以后,webform也可以使用模型绑定和模型验证. user实体: public class User { [Required] [Display(Name = "用户ID&q ...

  7. JS函数的参数声明中用 var 与不用 var的区别

    1.var 声明的变量,作用域是当前 function 2.没有声明的变量,直接赋值的话, 会自动创建变量,但作用域是全局的. 例如: function doSth() { a = "AAA ...

  8. 一个简单JDK动态代理的实例

    动态代理的步骤: 创建一个实现了InvocationHandler接口的类,必须重写接口里的invoke()方法. 创建被代理的类和接口 通过Proxy的静态方法 newProxyInsatance( ...

  9. npm安装删除模块以及cnpm淘宝镜像

    npm安装模块 [$ npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [$ npm install -g xxx]利用npm安装全局模块xxx: npm 删除模块 ...

  10. September,开启一个新的征程!

    寻找梦里的未来笑对现实的无奈不能后退的时候不再傍徨的时候永远向前 路...一直都在