HDU 1016:Prime Ring Problem
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25156 Accepted Submission(s): 11234
Note: the number of first circle should always be 1.

You are to write a program that completes above process.
Print a blank line after each case.
6
8
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
</pre><pre name="code" class="cpp">#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; int date[25]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int ans[25];
int n, cnt;
int flag; void dfs(int step)
{
int temp;
if(step==n) //深搜究竟时注意推断首尾是否符合要求
{
flag=1;
temp = ans[0]+ans[n-1];
if(flag)
{
for(int i=2; i*i<=temp; i++)
if(temp%i==0)
{
flag=0;
break;
}
}
if(flag)
{
printf("%d", ans[0]);
for(int i=1; i<n; i++)
printf(" %d", ans[i]);
printf("\n");
}
}
else
{
for(int i=1; i<n; i++)
{
if(date[i])
{
temp=ans[cnt-1]+date[i];
flag=1;
for(int j=2; j*j<=temp; j++)
{
if(temp%j==0)
{
flag=0;
break;
}
}
if(flag)
{
ans[cnt++]=date[i];
date[i]=0;
dfs(step+1);
date[i]=ans[--cnt]; //记得将数据的还原处理
ans[cnt]=0;
}
}
}
}
} int main()
{
int count=1;
while(scanf("%d", &n)!=EOF)
{
memset(ans, 0, sizeof(ans));
cnt=1;
ans[0]=1;
printf("Case %d:\n",count++);
dfs(1); //从1開始进入搜索
printf("\n"); //每次输出后记得加个换行
} return 0;
}
HDU 1016:Prime Ring Problem的更多相关文章
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 九度oj 题目1459:Prime ring problem
题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...
- DFS:Prime Ring Problem(素数环)
解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...
- 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(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem HDU - 1016 (dfs)
Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...
随机推荐
- Tor Browser(洋葱浏览器)——一款使你匿名上网的浏览器
第一次知道Tor是在有关斯诺登的新闻报道中. 简单说Tor是一款是你匿名访问网络的的软件.用户通过Tor可以在因特网上进行匿名交流. 为了实现匿名目的,Tor把分散在全球的计算机集合起来形成一个加密回 ...
- linux如何后台运行进程,而且不随终端关闭而关闭
参考:http://www.cnblogs.com/kaituorensheng/p/3980334.html 使用命令nohup:no hang up,表示关闭终端后,进程并不随着终端关闭而关闭 f ...
- 2016/4/14腾讯Android实习面试
腾讯实习面试,面试官会先问你会什么,擅长什么.然后会依照这个问,问的也不是非常偏,问道你不会的就换下一个知识点. 主要问我的有这几个问题 图形学 1.Bizer曲线怎么绘制的.原理是什么 2.对栅格有 ...
- Hydra 无法爆破SSH 解决办法
今天测试ssh爆破,发现使用hydra有些问题,windows版本没有协议支持其他的貌似都可以,kali本身也有hydra环境但是也会出现问题,所以就搜了一些资料贴在这里,当然这也是我测试过的,重新编 ...
- ActiveMQ的环境搭建及使用
一:环境搭建 ActiveMQ官网下载mq在windows上的安装包:http://activemq.apache.org/,解压到某个磁盘下. 运行要环境条件:jdk安装1.8,(本人这里安装版本) ...
- Elasticsearch入门基础(1)
基础概念 Elasticsearch有几个核心概念.从一开始理解这些概念会对整个学习过程有莫大的帮助. 接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着 ...
- 【python】下载网络文件到本地
# 下载网络图片文件到本地 import urllib.request rsp=urllib.request.urlopen("http://n.sinaimg.cn/ent/transfo ...
- vb.net Bitmap Drawing
Public Class Form1 Dim BMP As New Drawing.Bitmap(640, 480) Dim GFX As Graphics = Graphics.FromImage( ...
- Xcode5 打包 发布配置
http://www.cnblogs.com/zhaoqingqing/p/3553750.html 主题 Unity导出Xcode项目,使用Xocde打包ipa并提交到AppStore 步骤 1.设 ...
- Cg入门21:Fragment shader - 2D纹理採样
体纹理:是啥? tex2D 曾经仅仅能在Fragment程序中纹理採样 UV坐标系:事实上点为左下角,范围为[0,1].U为x轴,V为y轴 watermark/2/text/aHR0cDovL2Jsb ...