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 ...
随机推荐
- AndroidManifest.xml文件解析(转帖)
原帖地址:http://www.cnblogs.com/pilang/archive/2011/04/20/2022932.html 一.关于AndroidManifest.xml And ...
- 【转】QT中QDataStream中浮点数输出问题
先上代码: C/C++ code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 int main(int argc, char *argv[]) { QApplicati ...
- 在Linux中查看正在运行哪些process,杀掉一批名字相同的process
列出全部进程: ps -A 杀掉所有名为netmist的进程 killall netmist 参考资料 ============ https://www.howtogeek.com/107217/ho ...
- 插件化 VirtualAPK 简介 体验 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Spring boot Unable to start embedded Tomcat报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()
Spring boot Unable to start embedded Tomcat 报错 java.lang.NoSuchMethodError: javax.servlet.ServletCon ...
- Mysql 创建表时错误:Tablespace for table `tablexx` exists. Please DISCARD the tablespace before IMPORT.
在本地的一个数据库创建表时意外的出现了以下错误,说是表已经存在了 但通过desc 查看表信息时又说表不存在 在本地系统搜索表名却发现一个.ibd文件[InnoDB File] 在删除了该.ibd文件文 ...
- mysqli
CREATE TABLE `user` ( `id` ) NOT NULL, `name` ) NOT NULL, `password` ) NOT NULL ) ENGINE=InnoDB DEFA ...
- mycat 不得不说的缘分
.尾声.左兄与任正非.leader-us与马云 新成立的公司里面.有个左兄,非常传奇,大一在大学入伍,然后复员专业.来上海学IT.年纪轻轻,睡在地铁站,苦心专研数据库.系统.中间件,如今已经成为了业界 ...
- ArcEngine中COM对象与其基础RCW分开后就不能再使用
操作ArcEngine中的COM对象时,为了减少内存的增长,用掉的对象要手动释放常用的方法是ReleaseComObject System.Runtime.InteropServices.Marsha ...
- C# list与数组互相转换
1,从System.String[]转到List<System.String>System.String[] str={"str","string" ...