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. String作为输出型参数时获取不到值

    有时候在一个方法中,我们需要返回多个字符串,而又不想将这些字段包成一个类.此时就需要使用输出型参数. 但是如果将输出型参数的类型声明为String,那么调用该方法后,是获取不到我们想要的值的. 测试代 ...

  2. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  3. 2015/9/4 Python基础(8):映射和集合类型

    Python里唯一的映射类型是字典.映射类型对象里,hash值(key)和指向的对象(值)是一对多的关系.字典对象是可变的,这一点上很像列表,它也可以存储任意个数任意类型的Python对象,其中包括容 ...

  4. [USACO13FEB]出租车Taxi

    洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...

  5. 局部性原理的点滴应用场景 use of localityprinciple

    话说九月份博士入学面试的时候被问到了一个问题:请说明一下局部性原理在计算机科学中的应用场景?(哈哈,不记得怎么问的了,大概是这个意思)但是巴拉巴拉整半天却也只说出了一个Cache,后来补充的也都是跟C ...

  6. 原生js addclass,hasClass,removeClass,toggleClass的兼容

    (function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...

  7. Jmeter 重要测试指标释义

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  8. redhat 7 配置yum本地源

    http://www.unixarena.com/2015/04/how-to-create-the-yum-repository-on-rhel-7.html   1. 在虚拟机上挂上cd 2. m ...

  9. 使用JQGrid 问题汇总 不定时更新

    jqgrid左下角的复杂搜索框显示为下拉框样式searchoptions: { value: ": 全部; 1: 在用; 2: 报废", sopt: ['eq'] } jqgrid ...

  10. jsp之jstl核心标签库

    JSTL核心标签库技术 1. JSTL介绍 在JSP页面中即可书写html,也可以书写Java代码,导致页面混乱,维护,修改,升级难度加大,于是国际上不同的公司在实际应用中,根据页面的需求将Java代 ...