DFS:Prime Ring Problem(素数环)
解体心得:
1、一个回溯法,可以参考八皇后问题。
2、题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序。不用去特意排序。
3、输出有个坑,就是在输出一串的最后不能有空格,不然要PE,很尴尬。
题目:
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 3 8 5 2
Source
Asia 1993 8 5 2
Source
Asia 19 Shanghai (Mainland China)
#include<stdio.h>
#include<cstring>
int n,num[21],sum,l,sum1;
bool prim[50];
bool bnum[21];
int circle[21];
int z = 1;
void dfs(int sum)
{
if(sum == n)
{
if(!prim[circle[1]+circle[sum]])//判断第一个和最后一个相加是否是素数
return;
for(int j=1;j<=n;j++)
{
if(j!=n)
printf("%d ",circle[j]);//最后一个数字后面不可以加空格,不然要PE,很尴尬;
else
printf("%d",circle[j]);
}
printf("\n");
return;
}
for(int i=2;i<=n;i++)
{
if(!bnum[i])
continue;
if(prim[i+circle[sum]])
{
circle[sum+1] = i;
bnum[i] = false;
dfs(sum + 1);
bnum[i] = true;
}
}
return;
}
int main()
{
//先把50以内的素数手写出来
memset(prim,false,sizeof(prim));
prim[2] = true;
prim[3] = true;
prim[5] = true;
prim[7] = true;
prim[11] = true;
prim[13] = true;
prim[17] = true;
prim[19] = true;
prim[23] = true;
prim[29] = true;
prim[31] = true;
prim[37] = true;
prim[41] = true;
prim[43] = true;
prim[47] = true;
while(scanf("%d",&n)!=EOF)
{
//特判几个很大但是没有任何结果的数,直接输出,节省时间
if(n == 19 || n == 15 || n == 11 || n == 17 || n == 13)
{
printf("Case %d:\n",z++);
printf("\n");
continue;
}
printf("Case %d:\n",z++);
sum1 = 0;
l = 1;
sum = 1;
memset(circle,0,sizeof(circle));
memset(bnum,true,sizeof(bnum));
bnum[1] = false;
circle[1]=1;
dfs(1);
printf("\n");
}
}
DFS:Prime Ring Problem(素数环)的更多相关文章
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- Prime is problem - 素数环问题
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
- 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 + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- 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 ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 将MySQL转化为mysqli
<?php/** * Created by PhpStorm. * User: 大神 * Date: 2017/7/24 * Time: 11:29 */ header('content-typ ...
- SpringBoot | 第十五章:基于Postman的RESTful接口测试
前言 从上一章节开始,接下来的几个章节会讲解一些开发过程中配套工具的使用.俗话说的好,工欲善其事,必先利其器.对于开发人员而言,有个好用的工具,也是一件事半功倍的事,而且开发起来也很爽,效率也会提升很 ...
- Device
#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelega ...
- idea 清屏(控制台)快捷键
eclipse清屏快捷键为鼠标右键+R 而在idea中默认并没有清屏console的快捷键 所以需要我们自行设置: 1,ctrl+alt+s打开settings 2,找到keymap 3,搜索 cle ...
- 第6章 传输层(详解TCP的三次握手与四次挥手)
第6章 传输层 传输层简介 传输层为网络应用程序提供了一个接口,并且能够对网络传输提供了可选的错误检测.流量控制和验证功能.TCP/IP传输层包含很多有用的协议,能够提供数据在网络传输所需的必要寻址信 ...
- ajax 的dataType
这个问题已经碰到过好几次,经常自己挖坑,自己跳,而且还老是犯同样的错.一直纠结 明明其他地方得到的数据格式是对的.为什么这里就是不行.就是自己有洁癖去掉了 dataType:"json&q ...
- zblog添加水印插件后出现Cannot use $this as parameter
安装了水印插件后后台也进不去了,页面错误提示:Cannot use $this as parameter 删除水印插件文件后恢复正常,具体原因待研究 水印插件文件:/zb_users/plugin/W ...
- WPF学习一:XAML的资源(Resources)结构
一个初学者,把知识做个积累,如果有不对的地方,还请高手指出,谢谢! 先看一段代码:(下面是以Window WPF进行讲解,如果是Web 的话就把<Window改为<Page 而如果是 Us ...
- display:table的几个用法(元素平分宽度,垂直居中)
DIV+CSS的布局已经让表格布局几乎很少用到,除非表格语义性很强的情况. display:table解决了一部分需要使用表格特性但又不需要表格语义的情况, 尤其是DIV+CSS很不方便解决的问题,比 ...
- pta编程题19 Saving James Bond 2
其它pta数据结构编程题请参见:pta 题目 和简单版本不同的是,简单版本只需判断能否到达岸边,而这个版本要求求出最少跳数的路径. 简单版本用dfs实现,而这道题用BFS实现. 注意: 岛半径为7.5 ...