Prime Ring Problem

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

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
 

这道题就是回溯暴力。 首先打出一个素数表, 然后DFS回溯判断即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int is_prime(int x)
{
for(int i=; i*i<=x; i++)
if(x%i==) return ;
return ;
} int n, A[];
bool isp[], vis[];
void dfs(int cur)
{
if(cur==n&&isp[A[]+A[n-]])
{
for(int i=; i<n-; i++)
{
printf("%d ", A[i]);
}
printf("%d\n", A[n-]);
}
else for(int i=; i<=n; i++)
if(!vis[i]&&isp[i+A[cur-]])
{
A[cur] = i;
vis[i] = ;
dfs(cur+);
vis[i]=;
}
} int main()
{
int kase = ;
while(scanf("%d", &n)!=EOF)
{
printf("Case %d:\n", ++kase);
for(int i=; i<=n*; i++)
isp[i] = is_prime(i);
memset(vis, , sizeof(vis));
A[] = ;
dfs();
printf("\n");
}
return ;
}
 

HDU1016 Prime Ring Problem(DFS回溯)的更多相关文章

  1. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  2. hdu1016 Prime Ring Problem(DFS)

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

  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. hdu1016 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. Prime Ring Problem(dfs水)

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

  7. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  8. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  9. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

随机推荐

  1. Linux中文显示乱码解决

    输入 echo $LANG可以查看当前使用的系统语言 查看是否有中文语言包可以在终端输入 locale命令,如有zh cn 表示已经安装了中文语言 没有则 yum groupinstall chine ...

  2. ServiceStack.Redis 使用教程

    http://www.cnblogs.com/shanyou/archive/2011/11/10/2245082.html https://github.com/ServiceStack/Servi ...

  3. [转]Win7下安装配置sharepoint server 2010

    转自:http://blog.sina.com.cn/s/blog_5d93d7aa010151lp.html 要开发SharePoint 2010应用程序,开发人员必须构建一个SharePoint ...

  4. 2.1:你的第一个AngularJS App

    本章,带你体验一个简单的开发流程,将一个静态的使用模拟数据的应用,变成具有AngularJS特性的动态web应用.在6-8章,作者将展示如何创建一个更复杂,更真实的AngularJS应用. 1.准备项 ...

  5. easyui-tabs图标(获取焦点时显示图标,失去焦点时隐藏图标)

    获取焦点时显示图标,失去焦点时隐藏图标 <script type="text/javascript"> $('#_progress').tabs({ onSelect: ...

  6. Python 深拷贝和浅拷贝

    Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 下面本文就通过简单的例子介绍一下这些概念之间的差别. 对象赋值 直接看一段代码: will= ...

  7. 【转】Windows下搭建cvs服务器

    转载地址:http://hi.baidu.com/iloverobot/item/fad1eb6d66c45e166995e66d 下载cvs server:CVSNT 网址为:http://www. ...

  8. jQuery.validator 详解

    jQuery.validator 详解二 前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示 ...

  9. Buy Tickets

    Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16010 Accepted: 7983 Descript ...

  10. CyclicBarrier原理

    转载:http://www.cnblogs.com/skywang12345/p/3533995.html CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 ( ...