Prime Ring Problem

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

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
 

题意:输入一个 n 找出1~n的组合,使得相邻两个数之和为素数;
分析:预处理40之间的素数,然后回溯;
 #include<iostream>
#include<cstring>
#define N 25
#define M 40
using namespace std; bool is_prime[M],visited[N];
int n,test,ans[N]; void work(int k)
{
int i;
if(k==n+)
{
if(!is_prime[ans[n]+ans[]]) return ;
for(i=;i<=n-;i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
return ;
}
for(i=;i<=n;i++)
{
if(!visited[i]&&is_prime[ans[k-]+i])
{
visited[i]=true;
ans[k]=i;
work(k+);
visited[i]=false;
}
}
} bool prime(int n)
{
if(n==) return false;
if(n==||n==) return true;
int i;
for(i=;i<n;i++)
if(n%i==)
return false;
return true;
} int main()
{
int i;test=;
for(i=;i<M;i++) is_prime[i]=prime(i);
while(cin>>n)
{
ans[]=;
memset(visited,false,sizeof(visited));
cout<<"Case "<<test<<":"<<endl;
work();
test++;
cout<<endl;
}
return ;
}

hdu 1016 Prime Ring Problem(深度优先搜索)的更多相关文章

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

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

  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. [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(DFS)

    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 (回溯法)

    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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  9. hdu 1016 Prime Ring Problem(dfs)

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

随机推荐

  1. chart crash

    * thread #155: tid = 0x1fcc10, 0x0000000107626745 gpxj`static gpxj.ChartUtils.decimals (gpxj.ChartUt ...

  2. RDIFramework.NET — 基于.NET的快速信息化系统开发框架 - 5.3 数据库连接管理模块

    RDIFramework.NET — 基于.NET的快速信息化系统开发框架 5.3 数据库连接管理模块 5.3 数据库连接管理模块 我们经常可以看到很多软件直接把数据库连接字符串放在软件执行目录下的配 ...

  3. C# 统计程序执行时间

    随便写写,小马哥勿怪 最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.     话说马云年轻的时候也是屌丝一枚,有图为证 现 ...

  4. networkcommon v3开源消息框架资料

    http://www.cnblogs.com/csdev/category/870400.html

  5. C++Primer 第一章

    /* 1.main函数的标准写法就只有两种,一种是带命令行的,一种是不带命令行的.其返回类型必须是int. 2.如果main函数最后没有return语句,则编译器会自动加上一句 return 0; * ...

  6. 转:python webdriver API 之对话框处理

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个 iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是 div 形式的,这就让我们的处理变得 ...

  7. ACM之Java速成(3)

    ACM中Java.大数处理 先上个代码: import java.math.*; import java.util.*; class Main{ public static void main(Str ...

  8. RubyMine(基于IntelliJ IDEA的Ruby集成开发环境)快捷键

    常用快捷键: 一级必会Ctrl+Alt+G:弹出GenerateCtrl+Alt+L:格式化代码Alt+F1:切换视图(Project, Structure, etc.).Alt+F2:弹出预览窗口, ...

  9. EBS 密码相关

    SELECT usr.user_name, apps.cux_fnd_web_sec.decrypt ((SELECT (SELECT apps.cux_fnd_web_sec.decrypt (fn ...

  10. SQL Server session故障排查

    --根据作业名 查找session id select a.spid,a.blocked,b.name,substring(replace(a.PROGRAM_NAME,'SQLAgent - TSQ ...