[HDOJ1016]Prime Ring Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016
原题:
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.

输入N个数字,求1到N这N个数字连成环以后两两相加均为素数的所有情况。
非常经典的DFS题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
int cir[];
int prime[];
int vis[];
int isPrime(int x)
{
int i;
int sqx = sqrt((float)x);
for(i = ; i <= sqx; i++)
{
if(x % i == )
{
return ;
}
}
return ;
} void dfs(int x)
{
int i;
if(x == n && prime[cir[]+cir[n]]) //make a circle
{
for(i = ; i < n; i++)
{
printf("%d ", cir[i]);
}
printf("%d\n", cir[n]);
return ;
}
for(i = ; i <= n; i++)
{
if(vis[i] == && prime[cir[x]+i])
{
cir[x+] = i;
vis[i] = ;
dfs(x+);
vis[i] = ; //reset
}
}
} int main()
{
int i, Case = ;
for(i = ; i < ; i++) //list primes.
{
if(isPrime(i))
{
prime[i] = ;
}
} while(scanf("%d", &n) != EOF && n)
{
printf("Case %d:\n", Case++);
memset(vis, , sizeof(vis));
cir[] = ;
vis[] = ;
dfs();
printf("\n");
}
return ;
}
[HDOJ1016]Prime Ring Problem的更多相关文章
- HDOJ1016 Prime Ring Problem(DFS深层理解)
Prime Ring Problem 时间限制: 200 ...
- HDOJ-1016 Prime Ring Problem(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- yii2购物车实现
1.商品列表中点击加入购物车,则跳转到购物车列表,效果如图所示: 视图代码goods/list.php中.代码如下: <?php echo Html::a('加入购物车',['cart','id ...
- 深入Java核心 探秘Java垃圾回收机制(转自http://edu.21cn.com/java/g_189_859836-1.htm)
垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...
- 【SPFA】 最短路计数
最短路计数 [问题描述] 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. [输入格式] 输入第一行包含2个正整数N,M,为图的顶点数与边数. ...
- javascript 正则表达式(二)
/* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法: regexp对象实例.t ...
- OpenStack 多台计算节点时的问题
Contents [hide] 1 前言 2 bug 3 解决方法 4 网络问题 前言 添加一台计算节点后无法创建虚拟机.在调度层就错误: bug https://review.openstack.o ...
- android 项目学习随笔十四(WebView)
1.布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- 启用 TStringGrid 的自画功能,并避免重影
FMX 控件的 TStringGrid 下,有时为了让不同行或不同 Cell 的显示颜色.字体等有各种不同的颜色, 必须采用自画, 即在其 OnDrawColumnCell 事件中写自己的控制代码显示 ...
- linux-kernel 学习计划
[资料] http://www.ibm.com/developerworks/cn/views/linux/libraryview.jsp http://www.kerneltravel.net/ [ ...
- pstack使用和原理【转】
转自:http://www.cnblogs.com/mumuxinfei/p/4366708.html 前言: 最近小组在组织<<深入剖析Nginx>>的读书会, 里面作者提到 ...
- ServiceStack.Redis 使用教程
http://www.cnblogs.com/shanyou/archive/2011/11/10/2245082.html https://github.com/ServiceStack/Servi ...