Prime Ring Problem

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

Total Submission(s): 25187    Accepted Submission(s): 11246

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<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define M 25
using namespace std;
int vis[M],n,ans[M],t=0;
int sum(int a,int b)//判定素数的函数
{
int k=0,sum,i;
sum=a+b;
for(i=2;i<sum;i++)
{
if(sum%i==0)
{
k=1;
return 0;
}
}
if(!k)
return 1;
} void dfs(int x)
{
int i; if(x==n&&sum(ans[0],ans[n-1]))//递归边界 判定首尾是否符合条件
{ for(i=0;i<n;i++)
{ if(i==0)
cout<<ans[0];
else
cout<<" "<<ans[i]; }
cout<<endl;
}
else
{
for(i=2;i<=n;i++)
{
if(!vis[i]&&sum(i,ans[x-1]))
{
{
ans[x]=i;
vis[i]=1;//标记用过的数
dfs(x+1);
vis[i]=0;//还原用过的数
}
} }
}
}
int main()
{
int i,j;
while(cin>>n)
{ t++;
memset(vis,0,sizeof(vis));
cout<<"Case "<<t<<":"<<endl;
vis[1]=1;ans[0]=1;
dfs(1);
cout<<endl;//每一个例子之间有一个空行
} return 0;
}

杭电 1016 Prime Ring Problem的更多相关文章

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

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

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

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

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

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

  4. 杭电oj 1016 Prime Ring Problem

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

  5. hdu 1016 Prime Ring Problem(DFS)

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

  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(深度优先搜索)

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

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

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

随机推荐

  1. Javascript万物皆对象?

    在javascript的世界里,有这么一句话,万物皆对象. 但是这个对象,应该怎么理解呢? exm........??,难道值类型也是对象?!! 当然,不是. 准确地讲是对于“引用类型”而言. 那,在 ...

  2. js判读周末以及节假日

    两个链接,搞定! http://bbs.csdn.net/topics/10417668 http://www.jb51.net/article/131660.htm

  3. linux编译安装ccache3.2.4

    1.下载ccache3.2.4安装包 #cd /opt #wget http://samba.org/ftp/ccache/ccache-3.2.4.tar.gz 2.解压 #.tar.gz 3.创建 ...

  4. 基于 Web 的 Go 语言 IDE - Wide 1.4.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE . 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈. Lint 实时运行:极速编 ...

  5. SQL Server数据库备份的几个建议

    1.定期进行数据备份(完备或差异备份)和日志备份. 2.使用压缩备份来减少磁盘空间占用和提高备份效率. 3.定期检查磁盘剩余空间和备份文件增长情况,以确保有足够空间进行下一次备份. 4.使用校验和(C ...

  6. highcharts例子

    直接看代码 <script language="JavaScript"> $(document).ready(function() { $.ajax({ type:'p ...

  7. 读书笔记「Python编程:从入门到实践」_2.变量和简单数据类型

    做了大半年RPA了,用的工具是Kapow. 工作没有那么忙,不想就这么荒废着,想学点什么.就Python吧. 为期三个月,希望能坚持下来. 2.1 变量的命名和使用 变量名只能包含字母.数字和下划线. ...

  8. JS 用+1、-1填12()34()56()78()9=59

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. day26 hashlib, logging

    目录 hashlib hmac uuid logging v1 v2 v3 看了这个,上面的当作没看过 hashlib 为了防止密码在传输过程被抓取 对字符进行加密,相当于是一个自定义的字符编码表 原 ...

  10. Go 语言一本通

    什么是GO语言? Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...