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. Linux经常使用的命令(必看)

    http://www.importnew.com/12425.html     http://www.importnew.com/13107.html http://www.importnew.com ...

  2. MySQL JDBC的queryTimeout坑

    遇到一个MySQL JDBC跑execute规定的方法queryTimeout坑,更恶心,无论是BUG,不能,^_^,为什么要说?请看下面的说明: 现象: 用同一个Connection运行大批量SQL ...

  3. SQL Server 2008杀数据库连接

    杀数据库连接: DECLARE @temp NVARCHAR(20) DECLARE myCurse CURSOR FOR SELECT spid FROM sys.sysprocesses WHER ...

  4. 左右c++与java中国的垃圾问题的分析与解决

    左右c++与java中国的垃圾问题的分析与解决 DionysosLai(906391500@qq.com)  2014/8/1 问题分析: 之所以会出现中文乱码问题,归根结底在于中文的编码与英文的编码 ...

  5. 4、Cocos2dx 3.0三,找一个小游戏开发Hello World 分析

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 Hello World 分析 打开新 ...

  6. sql小总结2

    SQL NULL 值 如果表中的某个列是可选的,那么我们可以在不向该列添加值的情况下插入新记录或更新已有的记录.这意味着该字段将以 NULL 值保存. NULL 值的处理方式与其他值不同. NULL ...

  7. kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据

    kendo ui单击取消编辑数据buttongrid数据缩减.原因grid编辑数据追打datasorce于data寻找阵列数据的存在.假定有不会加入,加入缺席. 首先一个样本: html代码: < ...

  8. xdebug的安装和配置方法

    首先让php错误显示,仅仅须要改动php.ini其中的2条指令,把 displayerrors和htmlerrors都设置为On,例如以下所看到的 html_errors = On        di ...

  9. mysql_自动备份

    1 @echo off 2 set errorlevel=0 3 set dump_command="C:\Program Files\MySQL\MySQL Server 5.5\bin\ ...

  10. 它们的定义app.config中间section节点和在执行中使用

    如果现在我们需要在app.config一个节点的在下面的例子中,定义,我们需要如何进行操作? <configSections> <section name="integra ...