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. IOS中大文件拷贝算法

    + (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath { //每次读取数据大小 #define READ_SI ...

  2. Android LCD(二):常用接口原理篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/9125799 关键词:Android LCD TFT TTL(RGB)  LVDS  E ...

  3. vmxnet3 丢包处理

    https://vswitchzero.com/2017/09/26/vmxnet3-rx-ring-buffer-exhaustion-and-packet-loss/

  4. Go 文件操作

    一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...

  5. Docker 数据管理-tmpfs mounts

    Use tmpfs mounts Volumes and bind mounts are mounted into the container’s filesystem by default, and ...

  6. POJ 3159 最短路 SPFA

    #include<iostream> using namespace std; const int nMax = 30005; const int mMax = 150005; const ...

  7. 【codevs2333】&【BZOJ2002】弹飞绵羊[HNOI2010](分块)

    我其实是在codevs上看到它的题号后才去做这道题的...2333... 题目传送门:codevs:http://codevs.cn/problem/2333/ bzoj:http://www.lyd ...

  8. 解析3级JSON的例子

    我们都知道现在Ajax盛行,而且前后台数据交流的格式已经换成了JSON了.虽然我对这种做法还是有点担忧的,如果用户关闭了JavaScript怎么办?但是这些担忧还是不能阻止Ajax的盛行和JSON数据 ...

  9. SqlCacheDependency:asp.net SQL缓存依赖

    先看下MSDN对此类的介绍: 在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项:二是特定 SQL Server 数据库表或  SQL Server 2005 查询 ...

  10. BZOJ 2752 [HAOI2012]高速公路(road):线段树【维护区间内子串和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2752 题意: 有一个初始全为0的,长度为n的序列a. 有两种操作: (1)C l r v: ...