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+剪枝)的更多相关文章

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

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

  2. HDU 1016 Prime Ring Problem (DFS)

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

  3. Prime Ring Problem(dfs水)

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

  4. 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 ...

  5. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  6. hdu1016 Prime Ring Problem(DFS)

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

  7. 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 ...

  8. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  9. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

随机推荐

  1. webpack打包器简单入门

    概念 webpack是一个现代javascript应用程序的模块打包器. 当webpack处理你的应用程序时,它会递归构建一个依赖图(包含了你的应用程序所需要每个模块),然后把这些模块打包到少数几个b ...

  2. iOS开发-16进制颜色转换

    项目中经常会用到颜色转换,有的是通过十六进制转成数字转颜色,想简单的点直接通过字符串转一下,简单扩展了一下分类UIColor,代码如下: +(UIColor *)colorWithHex:(NSStr ...

  3. java Field 二三事

    java在反射时候经常用到了 Field 类. 我认为代码是最好的解释,直接上代码. 首先,用到了一个bean类,源代码如下: package com.ncs;      public class P ...

  4. 【Spark】Spark Streaming 动态更新filter关注的内容

    Spark Streaming 动态更新filter关注的内容 spark streaming new thread on driver_百度搜索 (1 封私信)Spark Streaming 动态更 ...

  5. 四轴自适应控制算法的一些尝试开源我的山猫飞控和梯度在线辨识自适应等算法—(转)

    本文的最主要目的在于抛砖引玉,阿莫论坛真的是非常好的一个论坛,没有这个论坛,没有那么多这个论坛上的前人无私的奉献和热烈的讨论,我想我是怎么也无法入门四轴的控制的.只是论坛上已经很多年都没有看到过新东西 ...

  6. 解决ThinkPHP的Create方法失效而没有提示错误信息的问题

    ThinkPHP中的数据创建Create方法是一个非常有用的功能,它自动根据表单数据创建数据对象(在表字段很多的情况下尤其明显) 但有时候该方法可能并未按照你期望的来工作,比如方法不工作而且还没有提示 ...

  7. how-to-get-a-job-in-deep-learning

    http://blog.deepgram.com/how-to-get-a-job-in-deep-learning/ How to Get a Job In Deep Learning 22 SEP ...

  8. [Functional Programming] Using Last monoid with Maybe

    Imaging we have a deck of cards, eveytimes we need to pick one card from deck, the result we want to ...

  9. IIS 7启用static JSON文件能POST方法

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...

  10. 复习C语言:第一章

    复习C语言中的5中基本类型,以及各个类型占用了多少个字节: #include <stdio.h> int main(void) { printf("int=%d\n", ...