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.



Inputn (0 < n < 20).

OutputThe 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 <map>
#include <cmath>
int n,ans[]={};
using namespace std;
int vis[];
bool isnum(int x)
{
if(x<=)return false;
for(int i=;i<=sqrt(x);i++)
{
if(x%i==)return false;
}
return true;
}
void dfs(int k,int last)
{
if(k==n)
{
if(!isnum(ans[]+ans[n-]))return;
printf("%d",ans[]);
for(int i=;i<n;i++)
printf(" %d",ans[i]);
putchar('\n');
return;
}
for(int i=;i<=n;i++)
if(!vis[i]&&isnum(last+i))
{
vis[i]=;
ans[k]=i;
dfs(k+,i);
vis[i]=;
}
}
int main()
{
int k=;
while(cin>>n)
{
vis[]=;
printf("Case %d:\n",++k);
dfs(,);
putchar('\n');
}
}

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. Uva 552 Prime Ring Problem(dfs)

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

  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. HDU1016 Prime Ring Problem(DFS回溯)

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

随机推荐

  1. PHP求并集,交集,差集

    PHP求并集,交集,差集 一.总结 一句话总结:在php中如果我想要对两个数组进行如并集.交集和差集操作,我们可直接使用php自带的函数来操作如array_merge(),array_intersec ...

  2. response.sendRedirect跳转 jsp:forward跳转

    response.sendRedirect跳转 <% response.sendRedirect("online.jsp"); %> jsp:forward跳转 < ...

  3. bartender学习

    参考: 官网  https://www.seagullscientific.com/label-software/barcode-label-design-and-printing 文章 http:/ ...

  4. English trip -- MC(情景课)3 D

    xu言: have a nice weekend... sentences How many people are there in you family? they are 3 people in ...

  5. English trip -- VC(情景课)1 B Countries

    Vocabulary focus 核心词汇 Vo ca bu la ry   fo cus [və(ʊ)'kæbjʊlərɪ]      ['fəʊkəs] Listen and repeat  听并 ...

  6. PHP函数总结 (一)

    <?php /** * 原理: * 函数不调用不执行,定义函数时,会将 * 函数放到内存中代码段,当调用函数时去内存 * 中函数名称所在位置中执行函数体,执行完后 * 将控制权移交回给调用函数的 ...

  7. C/C++中的实参和形参,重点以及盲点,自己以前未知的

    C/C++中的实参和形参   今天突然看到一道关于形参和实参的题,我居然不求甚解.藐视过去在我的脑海里只有一个参数的概念,对于形参和实参的区别还真的不知道,作为学习了几年C++的人来说,真的深深感觉对 ...

  8. python-day8-赋值

    # x=10 #链式赋值# a=b=c=d=e=f=10# print(a,b,c,d,e,f) #增量赋值 # x=10# y='a'# temp=x# x=y# y=temp# print(x,y ...

  9. WebService学习总结(转)

    原文地址: WebService学习总结(一)——WebService的相关概念 WebService学习总结(二)——WebService相关概念介绍 WebService学习总结(三)——使用JD ...

  10. OC 归档和解档

    #import <Foundation/Foundation.h> #define PATH @"/Users/mac/Desktop/file.txt" int ma ...