Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25156    Accepted Submission(s): 11234

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
</pre><pre name="code" class="cpp">#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; int date[25]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int ans[25];
int n, cnt;
int flag; void dfs(int step)
{
int temp;
if(step==n) //深搜究竟时注意推断首尾是否符合要求
{
flag=1;
temp = ans[0]+ans[n-1];
if(flag)
{
for(int i=2; i*i<=temp; i++)
if(temp%i==0)
{
flag=0;
break;
}
}
if(flag)
{
printf("%d", ans[0]);
for(int i=1; i<n; i++)
printf(" %d", ans[i]);
printf("\n");
}
}
else
{
for(int i=1; i<n; i++)
{
if(date[i])
{
temp=ans[cnt-1]+date[i];
flag=1;
for(int j=2; j*j<=temp; j++)
{
if(temp%j==0)
{
flag=0;
break;
}
}
if(flag)
{
ans[cnt++]=date[i];
date[i]=0;
dfs(step+1);
date[i]=ans[--cnt]; //记得将数据的还原处理
ans[cnt]=0;
}
}
}
}
} int main()
{
int count=1;
while(scanf("%d", &n)!=EOF)
{
memset(ans, 0, sizeof(ans));
cnt=1;
ans[0]=1;
printf("Case %d:\n",count++);
dfs(1); //从1開始进入搜索
printf("\n"); //每次输出后记得加个换行
} return 0;
}

HDU 1016:Prime Ring Problem的更多相关文章

  1. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. 九度oj 题目1459:Prime ring problem

    题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...

  3. DFS:Prime Ring Problem(素数环)

    解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...

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

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

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

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

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

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

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

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

  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. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

随机推荐

  1. 获取easyui calendar 属性

    <div id="cc" class="easyui-calendar" style="width:250px;height:250px;&qu ...

  2. [Android Pro] Android应用性能测试之CPU和内存占用(转载)

    首先稍做分析一下测试环境:我们知道CPU和内存占用是一个实时变化的状态,而市面上还没有具体的哪款android应用能做到实时监控CPU和内存占用并使用log日志保存.考虑到android的底层框架是基 ...

  3. Python学习(六)模块

    Python 模块 模块定义 随着程序越来越庞大,需要分隔成几个文件:也需要能在不同文件中复用函数.类和变量,而不是拷贝代码.为了满足这些需要,Python提供了模块. 简单来说,模块就是一个保存了P ...

  4. MYSQL三个默认库的介绍

    数据库INFORMATION_SCHEMA:提供了访问数据库元数据的方式. 元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时候用于表述该信息的其他术语包括“数据词典”和“系 ...

  5. centos6.x升级glibc-2.17

    glibc glibc是GNU发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc:它本身也提供了许多其它一些必要功能服务的实现: libc ...

  6. leetcode笔记:First Bad Version

    一. 题目描写叙述 You are a product manager and currently leading a team to develop a new product. Unfortuna ...

  7. RS报表从按月图表追溯到按日报表

    相信很多COGNOS开发人员看到这个标题就会感觉很轻松,追溯无非是COGNOS自带的一个下钻的功能,但是这里却是固定的条件: 要求1:A报表显示按月的图表B报表显示按日的明细 2:追溯到B的时候B的开 ...

  8. UNIX网络编程读书笔记:基本SCTP套接口编程

    概述 SCTP是一个较新的传输协议,于2000年在IETF得到标准化(TCP是在1981年标准化的).它最初是为满足不断增长的IP电话市场设计的:具体地说,就是穿越因特网传输电话信令. SCTP是一个 ...

  9. 【转】如何成为Python高手

    http://www.aqee.net/how-to-become-a-proficient-python-programmer/ 这篇文章主要是对我收集的一些文章的摘要.因为已经有很多比我有才华的人 ...

  10. java.lang.IllegalArgumentException: taglib definition not consistent with specification version

    web.xml报错 taglib标签错误,3.0要用jsp-config <jsp-config>    <taglib>        <taglib-uri>& ...