Prime Ring Problem

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

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

 
Source
 
 
 
 
 
代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int prime[]={,,,,,,,,,,,};
bool isprime[];
bool used[];
int num[],n;
bool dfs(int count,int cur){ //深度优先搜索
num[count]=cur;
if(count==n-){
if(!isprime[cur+])return false;
for(int i=;i<n-;i++)
printf("%d ",num[i]);
printf("%d\n",num[n-]);
return false;
}
for(int i=;i<=n;i++){
if(used[i])continue;
used[i]=true;
if(isprime[cur+i]&&dfs(count+,i))
return true;
used[i]=false;
}
return false;
}
int main(){
memset(isprime,false,sizeof(isprime));
for(int i=;i<;i++)
isprime[prime[i]]=true;//存在素数isprime数组标记为1
int Case=;
while(~scanf("%d",&n)){
printf("Case %d:\n",Case++);
memset(used,false,sizeof(used));
used[]=true;
dfs(,);
printf("\n");
}
return ;
}

HDU 1016.Prime Ring Problem-素数环,相邻两数和为素数-DFS的更多相关文章

  1. hdu 1016 Prime Ring Problem (素数环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. #include <iost ...

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

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

  3. [HDU 1016]--Prime Ring Problem(回溯)

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

  4. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  5. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  6. hdu 1016 Prime Ring Problem(DFS)

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

  7. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  8. hdu 1016 Prime Ring Problem(深度优先搜索)

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

  9. HDU 1016 Prime Ring Problem (回溯法)

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

随机推荐

  1. 第01篇 说一下Setting,我一直没有讲过

        settings 调整 settings 中的设置是非常关键的,它们会改变 MyBatis 的运行时行为.下表描述了设置中各项的意图.默认值等. 设置参数 描述 有效值 默认值 cacheEn ...

  2. UVALive-4670 Dominating Patterns / 洛谷 3796 【模板】AC自动机

    https://vjudge.net/problem/UVALive-4670 中文题面:https://www.luogu.org/problem/show?pid=3796 AC自动机模板 注意如 ...

  3. iOS 点击cell上的按钮获取行数

    -(void)btnClick:(UIButton *)button{ UITableViewCell *cell = (UITableViewCell *)[[button superview] s ...

  4. python检测硬盘脚本

    #!/usr/bin/env python # _*_coding:utf-8_*_ import os import sys import statvfs def main(): '''deamon ...

  5. Markdown 代码块中再内嵌一个行内代码

    在 jQuery 1.9 之前(不含1.9):如果传入一个空字符串. null 或 jQuery.parseJSON( jsonString ) ,该函数将返回,而不是抛出一个错误,即使它不是有效的  ...

  6. java与C#的基础语法区别--持续更新

    1.判断字符串是否相等 java : equals()比较的是对象的内容(区分字母的大小写格式),但是如果使用“==”比较两个对象时,比较的是两个对象的内存地址,所以不相等.即使它们内容相等,但是不同 ...

  7. 20155335俞昆《java程序设计》第10周总结

    学号 2016-2017-2 <Java程序设计>第十周学习总结 ## 事实上网络编程,我们可以简单的理解为两台计算机相互通讯数据而已,对于程序员而言,掌握一种编程接口并使用一种编程模型相 ...

  8. HDU 1599 find the mincost route (最短路 floyd)

    题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...

  9. Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总

    In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...

  10. 8.0docker的客户端和守护进程

    C/S 链接的方式 unix:///var/run/docker.sock 默认的 tcp://host:port fd://socketfd 在linux上进行socket 模拟 nc -U /va ...