Prime Ring Problem

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

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
 
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
int n,buf[MAXN],vis[MAXN];
bool isPrime(int x)
{
if(x<=) return false;
for(int i=;i*i<=x;i++)
{
if(x%i==) return false;
}
return true;
}
void dfs(int dep)
{
if(dep==n)
{
for(int i=;i<dep-;i++)
{
printf("%d ",buf[i]);
}
printf("%d\n",buf[dep-]);
return ;
}
for(int i=;i<=n;i++)
{
if(!vis[i]&&isPrime(i+buf[dep-]))
{
if(dep==n-&&!isPrime(i+buf[]))
continue;
vis[i]=;
buf[dep]=i;
dfs(dep+);
vis[i]=;
}
}
}
int main()
{
int cas=;
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
printf("Case %d:\n",++cas);
buf[]=;
dfs();
printf("\n");
}
return ;
}

HDOJ1016(标准dfs)的更多相关文章

  1. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  2. Prime Ring Problem (DFS练习题)

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

  3. HDU 1983 Kaitou Kid - The Phantom Thief (2)

    神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...

  4. 【BZOJ2019】nim

    直播写题这刺激233 原题: 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输. ...

  5. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  6. hdoj1016 [dfs]

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意: 已知一个数n,在1-n(包含 n ,0 < n < 20)里组合形成一个环形,使得每两 ...

  7. hdoj--1016<dfs>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述:1-n的整数排成一个环,首尾相连,相邻的两个数相加是素数,输出满足的排列,1开头输出, ...

  8. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  9. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

随机推荐

  1. cocos2dx打飞机项目笔记二:BulletLayer类

    BulletLayer.h 内容如下 class BulletLayer : public cocos2d::CCLayer { public: CC_SYNTHESIZE(bool, m_IsHer ...

  2. FilterDispatcher处理流程

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 前身是:org.apache.struts2.dispatc ...

  3. sp_rename sqlserver 表 列 索引 类型重命名

    --[语法] sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object ...

  4. java:RandomAccessFile随机读取文件内容

    RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须是可知的.但是该类仅限于操作文件. ...

  5. MinGW main()

    MinGW没有wmain入口函数,为了获取宽字符的参数,可以用系统API函数GetCommandLineW. main.cpp #include <iostream> #include & ...

  6. 安装Xcode 7 beta后Xcode 6崩溃的问题

    最新解决方案:把OSX El Capitan升级到Beta 7  (15A263e),Xcode6可使用! 解决方案:http://stackoverflow.com/questions/318035 ...

  7. hive_学习_00_资源帖

    一.官方资料 二.参考资料

  8. hdoj-1032-The 3n + 1 problem(坑题)

     题目链接 //巨坑的一道题,输入的m,n要判断大小,输出还要按照原来的顺序,范围还是i<=n<=j #include <iostream> #include <cstd ...

  9. hdu5087 Revenge of LIS II (dp)

    只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...

  10. IO编程、操作文件或目录、序列化、JSON

    IO中指Input/Output,即输入和输出:涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口 1.由于CPU和内存的速度远远高于外设的速度,所以,在IO编程中,存在速度严重不匹配问题.eg ...