Day2-M-Prime Ring Problem-HDU1016
Note: the number of first circle should always be 1.
Inputn (0 < n < 20).
OutputThe 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 思路:依旧是回溯法,逐个判断即可,注意首尾也需要判断一下,用筛法建个表,代码如下:
const int maxm = ; int n, vis[maxm], prime[], res[maxm], kase = ; void buildTable() {
for (int i = ; i * i < ; ++i) {
if(!prime[i]) {
for (int j = i * i; j < ; j += i)
prime[j] = ;
}
}
} void dfs(int i) {
if(i == n && !prime[res[] + res[n-]]) {
for (int j = ; j < n; ++j) {
if(j)printf(" ");
printf("%d", res[j]);
}
printf("\n");
}
for (int j = ; j <= n; ++j) {
if(!vis[j] && !prime[res[i-] + j]) {
vis[j] = ;
res[i] = j;
dfs(i + );
vis[j] = ;
}
}
} int main() {
buildTable();
while(scanf("%d",&n) != EOF) {
printf("Case %d:\n", ++kase);
memset(vis, , sizeof(vis));
res[] = ;
vis[] = ;
dfs();
printf("\n");
}
return ;
}
Day2-M-Prime Ring Problem-HDU1016的更多相关文章
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem素数环(HDU1016)
Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- day46_Webservice学习笔记_02
一.回顾昨天所学 什么是webservice? 什么是远程调用技术?答:系统和系统之间的调用,从远程系统当中获取业务数据. Webservice是web服务,他是用http传输SOAP协议 ...
- POJ-1087 A Plug for UNIX (网络流)
思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...
- Docker Learning Notes
Docker简介 是什么 问题:为什么会有docker出现 一款产品从开发到上线,从操作系统,到运行环境,再到应用配置.作为开发+运维之间的协作我们需要关心很多东西,这也是很多互联网公司都不得不面对的 ...
- java 类型转换一些相关问题
猜测:第二句 第四句会出错 结果是第二句和第四句会出错.说明了父类可以向子类类型转换,而不同的子类直接不能类型转换.
- 【转载】五分钟让你彻底了解TDD、ATDD、BDD&RBE
在目前比较流行的敏捷开发模式(如极限编程.Scrum方法等)中,推崇“测试驱动开发(Test Driven Development,TDD)”——测试在先.编码在后的开发实践.TDD有别于以往的“先编 ...
- 刚下载好的 vscode 不能运行,一片黑 以及终端不能输入 解决办法
1.鼠标右键vscode快捷方式点击属性,选择兼容性,勾选以兼容模式运行,下拉列表调整为windows vista (service pack 1)即可解决. 2.如果打开终端不能输入命令,首先点击属 ...
- 虚拟机下修改ip配置
// centos ip 配置vim /etc/sysconfig/network-scripts/ifcfg-eth0 // 虚拟机下删除里面的内容vim /etc/udev/rules.d/70- ...
- 剑指offer第二版速查表
3.数组中重复数字:每个位置放置数字与下标对应相等 O(n) 4.二维数组中的查找:右下角开始比较 O(m+n) 5.替换空格:python直接替换 6.从尾到头打印链表: 借助栈或直接利用系统调用栈 ...
- 吴裕雄--天生自然PYTHON爬虫:使用Selenium爬取大型电商网站数据
用python爬取动态网页时,普通的requests,urllib2无法实现.例如有些网站点击下一页时,会加载新的内容,但是网页的URL却没有改变(没有传入页码相关的参数),requests.urll ...
- 【转】CGI 和 FastCGI 协议的运行原理
介绍 深入CGI协议 CGI的运行原理 CGI协议的缺陷 深入FastCGI协议 FastCGI协议运行原理 为什么是 FastCGI 而非 CGI 协议 CGI 与 FastCGI 架构 再看 Fa ...