ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem
Time Limit: 10 Seconds
Memory Limit: 32768 KB
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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; int n,test=0;;
int cir[30];
int vis[30];
int prime[50]={0}; void dfs(int k)
{
if(k==n)
{
if(!prime[cir[k]+cir[1]])
return;
for(int i=1;i<n;i++)
printf("%d ",cir[i]);
printf("%d",cir[n]);
printf("\n");
return;
} for(int j=2;j<=n;j++)
{
if(!vis[j]&&prime[cir[k]+j]) //对于相邻素数的推断,在这里才干在zoj,AC,放最前面就仅仅能在HDU,AC了,一层递归调用的时间而已啊!
{ vis[j]=1;
cir[k+1]=j;
dfs(k+1);
vis[j]=0; //注意递归复原,这预计是最大的亮点了 } }
} int main()
{
prime[3]=1;
prime[5]=1;
prime[7]=1;
prime[11]=1;
prime[13]=1;
prime[17]=1;
prime[19]=1;
prime[23]=1;
prime[29]=1;
prime[31]=1;
prime[37]=1; while(scanf("%d",&n)!=EOF)
{
//memset(cir,0,sizeof cir);
//memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
{
vis[i]=0;
}
printf("Case %d:\n",++test);
if(n % 2 == 1)
{
printf("\n");
continue;
} cir[1]=1;
dfs(1);
printf("\n");
} return 0;
}
ZOJ 1457 Prime Ring Problem(dfs+剪枝)的更多相关文章
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 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 ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- ZOJ 1457 E-Prime Ring Problem
https://vjudge.net/contest/67836#problem/E A ring is compose of n circles as shown in diagram. Put n ...
- Prime Ring Problem dfs
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
随机推荐
- QT中使用MinGW 编译的protobuf库--包含库的生成和使用
QT中使用MinGW 编译的protobuf库--包含库的生成和使用 0前言 1准备工作 2生成protobuf库文件 3在QT中测试protobuf的使用 4结语 0前言 最近要在QT中使用prot ...
- 算法: skiplist 跳跃表代码实现和原理
SkipList在leveldb以及lucence中都广为使用,是比较高效的数据结构.由于它的代码以及原理实现的简单性,更为人们所接受. 所有操作均从上向下逐层查找,越上层一次next操作跨度越大.其 ...
- 关于MAX30100心率的编程
MAX30100是能够读取心率.血氧的传感器,通信方式是通过IIC进行通信. 其工作原理是通过红外led灯照射,能够得到心率的ADC值. 图为MAX30100的寄存器. 可以分为五类,状态寄存器.FI ...
- Windows Server 2008 R2 小技巧 (转)
一些 Windows Server 2008 R2 的小技巧,包括启用「God Mode (上帝模式)」.添加「快速启动」工具栏.启用桌面「个性化」服务.停用「密碼複雜性」要求,对老程序员熟悉新版的 ...
- easyui +ASP.NET 前后台乱码解决方法
前端接口 decodeURIencodeURI 后台接口 System.Web.HttpUtility.UrlEncode(user.name) 凡是涉及到中文的地方都要使用这几个接口
- DIV+CSS规范命名集合
我们开发CSS+DIV网页(Xhtml)时候,比较困惑和纠结的事就是CSS命名,特别是新手不知道什么地方该如何命名,怎样命名才是好的方法. 命名规则说明: 1).所有的命名最好都小写 2).属性的值一 ...
- 【Java】SAX解析characters 错误截取问题的解决
SAX解析characters 错误截取问题的解决 SAX characters bug_百度搜索 SAX解析characters 错误截取问题的解决 - CSDN博客 [Android]SAX解析之 ...
- NLP领域的ImageNet时代到来:词嵌入「已死」,语言模型当立
http://3g.163.com/all/article/DM995J240511AQHO.html 选自the Gradient 作者:Sebastian Ruder 机器之心编译 计算机视觉领域 ...
- Linq-排序Order By
适用场景:对查询出的语句进行排序,比如按时间排序等等. 说明:按指定表达式对集合排序:延迟,:按指定表达式对集合排序:延迟,默认是升序,加上descending表示降序,对应的扩展方法是OrderBy ...
- 使用 Edit + MASM 5.0 编译器 + Linker 连接器
其实这种方式是很简单的,只是很麻烦,因为简单而且麻烦, 所以我采用尽可能的将截图传上来,然后稍加注解的方式进行介绍, 软件准备: 需要 MASM 5.0 或者以上的汇编编译器 首先,是要编辑汇编源代码 ...