hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 74594 Accepted Submission(s): 31690
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
Source
Asia 1996, Shanghai (Mainland China)
题意大致就是说要找到由1~n组成的环,且相邻的数两两相加都要是素数,输出所有满足条件的序列。
AC代码如下:
#include<stdio.h>
#include<string.h>
int f[100]={0};
int ans[21],v[21],n;
void dfs(int i)
{
int m;
if(i==n&&f[ans[i-1]+ans[0]]==0) //找到8个满足条件的数时输出
{
for(m=0;m<n-1;m++)
printf("%d ",ans[m]);
printf("%d\n",ans[n-1]);
}
else
{
for(m=2;m<=n;m++) //找未用过的数
{
if(v[m]==0) //找到还没有用过的数
{
if(f[m+ans[i-1]]==0) //如果满足相邻数和为素数
{
v[m]=-1; //标记已用过
ans[i++]=m; //将m放进数组
dfs(i); //找下一个数
v[m]=0; //接触标记
i--; //回溯
}
}
}
}
}
int main()
{
int i,j,cases=0;
//素数打表
for(i=2;i<100;i++)
if(f[i]==0)
for(j=i;i*j<100;j++)
f[i*j]=1;
while(scanf("%d",&n)!=EOF)
{
cases++;
ans[0]=1;
printf("Case %d:\n",cases);
memset(v,0,sizeof(v));
dfs(1);
printf("\n");
}
return 0;
}
---------------------
作者:yongtaozheng
来源:CSDN
原文:https://blog.csdn.net/Twinkle_sone/article/details/95123292
版权声明:本文为博主原创文章,转载请附上博文链接!
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】的更多相关文章
- 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, -, ...
- 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 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 (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
- hdu1016 Prime Ring Problem
dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...
随机推荐
- Python 04 Geany的安装和配置
安装原文:https://www.cnblogs.com/wongyi/p/7832567.html 配置原文:https://jingyan.baidu.com/album/154b46311ed9 ...
- Glider(前缀和+二分)
题目链接:Glider Gym-101911B 解题分析:下落的高度一定,是h.在没有气流的地方每秒下落1:所以可以转化为经过无气流地带的时间总长为h. 那么很显然从一个有气流地带的开始,选择下落,那 ...
- Python TIPS上一道关于人民币金额小写转大写的题
人民币金额打印 题目链接:here.我发现我写的好复杂,但万幸编码还算符合人类,看了其他答案,感觉都是天书. #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- 咕泡学院java架构vip课程
1.wps文档地址 https://docs.qq.com/doc/DRVNLUndvTmFSdEhO 2.百度网盘地址 https://pan.baidu.com/s/1uxaTzJZHKrsw_H ...
- springboot带有进度条的上传
一.说明 最近要做文件上传,在网上找了很久都没有一个全面的示例,特此记录下来分享给大家. 1.文件上传接口可按照springboot默认实现,也可用commons-fileupload组件,本示例使用 ...
- udf也能用Python
具体步骤见<fluent加载第三方(C++,Fortran等)动态链接库> 我们对导入的动态链接库进行改动 打开VS2013 完成了上述过程以后,还需要配置Python 首先需要安装Pyt ...
- SDN上机第四次作业
1. 解压安装OpenDayLight控制器(本次实验统一使用Beryllium版本) 安装ODL后安装插件 2.3用Python脚本搭建如下拓扑,连接OpenDayLight控制器 拓扑如图: Py ...
- 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1
目录 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1 解题过程 刷题记录:[CISCN2019 总决赛 Day1 Web4]Laravel1 题目复现链接:https:/ ...
- Linux学习笔记(一)
操作系统(operation system,os) 主要作用是管理好硬件设备,并为用户和应用程序提供一个简单的接口, 以便于使用,作为中间人,连接软件和硬件 操作系统linux发展历程 unix-&g ...
- 从宿主机直接进入docker容器的网络空间
Docker dns nameserver 也是进入容器网络空间,监听53端口,但它通过iptable把端口映射到宿主机上,处理DNS请求的进程就在宿主机上. how does Docker Embe ...