[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 ...
随机推荐
- 导出excel部分代码
public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...
- 七、Java基础---------JDK安装与配置
配置环境变量名词说明 path:通过path系统去寻找可执行的java文件. JAVA_HOME:JDK的安装目录 classpath:加载目录 为什么需要配置path,什么时候需要classpath ...
- Hibernate中Session的get和load
hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别.测试版本:hibernate 4.2.0. get Session接 ...
- 关于路由器自定义 3322.org 的DDNS
首先, 3322.org, 现在官网地址为: http://www.pubyun.com/ 注册用户后,如果支持 3322 的路由器,可以直接设置. 不支持的路由就要想办法自定义了. 3322 的 D ...
- MySQL start and stop
一.本文说明 本实验主要是演示MySQL的四种启动方式,附带停止的操作. 二.mysqld mysqld is the MySQL server mysqld reads options from ...
- Word and MediaElement
function jmc_new_lib(){// Because we still want the script to load but not the styleswp_enqueue_scri ...
- PHP笔记随笔
1.CSS控制页面文字不能复制: body{-webkit-user-select:none;} 2.[php过滤汉字和非汉字] $sc="aaad....##--__i汉字过滤&quo ...
- easyui-tabs图标(获取焦点时显示图标,失去焦点时隐藏图标)
获取焦点时显示图标,失去焦点时隐藏图标 <script type="text/javascript"> $('#_progress').tabs({ onSelect: ...
- C#:控制台程序调用中间库创建窗体
1.类库项目引用System.Windows.Forms并添加引用后,才可创建窗体. 2.控制台应用程序调用中间库(DLL)中的方法创建窗体:中间类库使用反射下的Assembly加载包含窗体的类库及创 ...
- js函数自执行
在javascript里,任何function在执行的时候都会创建一个执行上下文,因为function声明的变量和function有可能只在该function内部,这个上下文,在调用function的 ...