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. FTPS加密上传

    公司要求ftp接口不能以明文方式传输,所以adc系统将增加ftps方式 但是在网找了很多方式都无法实现用了方法一 FtpWebRequest request = (FtpWebRequest)WebR ...

  2. 设置eclipse补全方法

    开MyEclipse 6.0. 1,然后“window”→“Preferences” 2,选择“java”,展开,“Editor”,选择“Content Assist”. 3,选择“Content A ...

  3. Keepalived原理及配置应用总结

    Keepalived——保持存活,在网络里的含义就是保持在线.Keepalived提供高可用和热备的功能,用来防止单点故障的发生. 1.VRRP协议基本原理介绍 Keepalived实现的基础是VRR ...

  4. C#:写日志

    一.直接写入文件 /// <summary> /// 将message的内容写入日志文件 /// </summary> /// <param name="msg ...

  5. hdwiki 的模板和标签

    模版和标签 HDWiki模版标签可以让用户更方便.更容易的在页面中调用HDWiki的数据.        HDWiki的三种标签:1.循环标签 {hdwiki:doclist...}{/hdwiki} ...

  6. css 样式设计(一)( 在线150个例子 | 背景 | 文本 | 字体 | 链接 | 列表 | 表格 | 盒模型 | 边框 | 轮廓 | 边距 | 填充 |分组和嵌套 | 尺寸 | 定位 | 浮动 |对齐 )

    一.css在线150个例子 http://www.w3cschool.cc/css/css-examples.html 二.背景图片水平方向重复 : body { background-image:u ...

  7. YTU 3022: 完全二叉树(1)

    原文链接:https://www.dreamwings.cn/ytu3022/2595.html 3022: 完全二叉树(1) 时间限制: 1 Sec  内存限制: 128 MB 提交: 26  解决 ...

  8. Bellman-Ford 算法及其优化

    Bellman-Ford 算法及其优化 转自:http://hi.baidu.com/jzlikewei/blog/item/94db7950f96f995a1038c2cd.html Bellman ...

  9. MySQL 数据库发展历程及缺点

    传统关系数据库的瓶颈 传统的关系数据库具有不错的性能,高稳定型,久经历史考验,而且使用简单,功能强大,同时也积累了大量的成功案例.在互联网领域,MySQL成为了绝对靠前的王者,毫不夸张的说,MySQL ...

  10. 2016年11月16日 星期三 --出埃及记 Exodus 20:7

    2016年11月16日 星期三 --出埃及记 Exodus 20:7 "You shall not misuse the name of the LORD your God, for the ...