Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20105    Accepted Submission(s): 9001

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
 
Recommend

JGShining

题意:素数环 (经典搜索)

代码:

写法1:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int yes[22];
bool in[22];
bool prim[50];
int n; void primtable()
{
int i,j;
memset(prim,0,sizeof(prim));
for(i=3;i<=50;i++)
{
for(j=2;j<=i-1;j++)
{
if(i%j==0)
prim[i]=true;
}
}
} void dfs(int pos)
{
int i,u;
if(pos>n)
{
int m=yes[1]+yes[n];
if(prim[m]==false)
{
for(i=1;i<=n;i++)
printf("%d%c",yes[i],i==n?'\n':' ');
//printf("\n"); //这里PE了一次
}
return;
}
for(i=2;i<=n;i++)
{
u=i+yes[pos-1];
if(prim[u]==false&&in[i]==false)
{
yes[pos]=i;
in[i]=true;
dfs(pos+1);
in[i]=false;
}
}
} int main()
{
int cas=1;
primtable();
while(scanf("%d",&n)!=EOF)
{
memset(in,0,sizeof(in));
yes[1]=1;
in[1]=true;
printf("Case %d:\n",cas++);
dfs(2);
printf("\n");
}
return 0;
} // 203MS

8765975

2013-07-30 16:19:31

Accepted

203MS

228K

1125 B

C++

写法2:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int yes[22];
bool in[22];
bool prim[50];
int n; void no_prim()
{
int tmp,i,j;
for(i=3;i<20;i+=2)
{
if(prim[i]==false)
{
tmp=i<<1;
for(j=i*i;j<50;j+=tmp)
prim[j]=true;
}
}
} void dfs(int pos)
{
int i,u;
if(pos>n)
{
int m=yes[1]+yes[n];
if(prim[m]==false&&m%2||m==2)
{
for(i=1;i<=n;i++)
printf("%d%c",yes[i],i==n?'\n':' ');
//printf("\n");
}
return;
}
for(i=2;i<=n;i++)
{ u=i+yes[pos-1];
if((!prim[u]&&u%2||u==2)&&in[i]==false)
{
yes[pos]=i;
in[i]=true;
dfs(pos+1);
in[i]=false;
}
}
} int main()
{
no_prim();
int cas=1;
while(scanf("%d",&n)!=EOF)
{
memset(in,0,sizeof(in));
yes[1]=1;
in[1]=true;
printf("Case %d:\n",cas++);
dfs(2);
printf("\n");
}
return 0;
} // 234MS

8765965

2013-07-30 16:18:52

Accepted

234MS

228K

1143 B

C++

Hdu 1016 Prime Ring Problem (素数环经典dfs)的更多相关文章

  1. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  2. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  3. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  4. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  5. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  7. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  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. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 基于PHP的crontab定时任务管理

    BY JENNER · 2014年11月10日· 阅读次数:6 linux的crontab一直是server运维.业务开展的利器.但当定时任务增多时,管理和迁移都变得非常麻烦,并且easy出问题.以下 ...

  2. 使用python做你自己的自动化测试--对Java代码做单元测试 (2)-导入第三方jar包裹

    使用Jython对Java做单元测试,当然,为了测试开发java代码.这涉及到引入第三包的问题,如何导入第三方的包? 您可以使用http://blog.csdn.net/powerccna/artic ...

  3. Nyoj 虚拟的城市之旅(bfs)

    描述   展馆是未来城市的缩影,个人体验和互动是不变的主题.在A国展馆通过多维模式和高科技手段,引领参观者在展示空间踏上一段虚拟的城市之旅. 梦幻国有N个城市和M条道路,每条道路连接某两个城市.任意两 ...

  4. 框架搭建资源 (一) V(视图)C(控制)模式

    pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncodin ...

  5. PB控制性能TreeView

    TreeView [其它] ■TreeView控件能够以树型方式来组织项目.不仅显示直观.界面友好.并且项目的管理和操作更为方便,是当前比較流行的一个控件. 该控件的特点是在较小的空间能够分层次显示大 ...

  6. 采用RedisLive监控Redis服务

    1.基础环境安装https://pypi.python.org/packages/source/b/backports.ssl_match_hostname/backports.ssl_match_h ...

  7. Python 得到Twitter所有用户friends和followers

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-29 @author: guaguastd @name: f ...

  8. Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading

    Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...

  9. RHEL5 X86-64上安装Oracle 11gR2演示样例与总结

    进入Oracle DBA行业也有好几年了,可是说到安装Oracle的经验,我还真不是特别多,印象中刚開始每次安装都有点磕磕碰碰,随着接触Oracle的时间越来越长,各方面的原理.机制也都有一定的了解后 ...

  10. hdu 5073 Galaxy(2014acm鞍山亚洲分部 D)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)   ...