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. Java使用Log4记录日志

    我们在系统使用中,为了方便查找问题,因此需要记录操作的日志,而目前比较成熟稳定的程序日志记录方式就是Log4,本人也是菜鸟,然后再学习研究中就记录一下使用方式,以方便今后查阅,同时本文章参考了博客园: ...

  2. 什么是SQL游标?

    1.1游标的概念 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集.使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式.用SQL语言从数据库中检索数据 ...

  3. [.NET开发] C#使用doggleReport生成pdf报表的方法

    本文实例讲述了C#使用doggleReport生成pdf报表的方法.分享给大家供大家参考,具体如下: 1. 安装nuget -install package DoddleReport -install ...

  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. Win7 默认.lnk打开方式全是别的程序 还原的办法

    Xu言: no zuo no die~ 今天,一个朋友问我,他电脑桌面上点任何东西都是提示下载... - -||| 本以为是中毒了,然后上去看了一眼..发现他自己把所有.lnk 的默认打开方式选择了搜 ...

  6. Visio 入门教程

    最近做一个新项目,目前在需求确立阶段,所以每天任务是写文档讨论再修改.由于是云端架构设计,避免不了图形图表配合文字说明,需要制作 E-R 图.网络图.时序图.UML 类图等,对比其他可视化图表工具,V ...

  7. 现在转战c++的领域,纯幼儿园

    C++中: 如果你用#include<iostream.h>就不需写这句话(旧标准).但是如果你用#include<iostream>就必须要写.但是在VS2010中就出现错误 ...

  8. java反编译工具eclipse插件jad的使用

    https://www.cnblogs.com/zhikou/p/8098137.html 这边文章主要介绍如何使用Eclipse的插件jad进行反编译,在查看一些jar包里面的class文件时,就可 ...

  9. PHP:第二章——PHP中的foreach语句

    foreach语句提供了遍历数组的 <?php header("Content-Type:text/html;charset=utf-8"); $arr=array(&quo ...

  10. vue 表格导出excel

    首先要install两个依赖, 1 npm install -S file-saver xlsx 2  npm install -D script-loader 在项目src目录下新建一个文件夹ven ...