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 ...
随机推荐
- Ruby:Sublime中开发Ruby需要注意的Encoding事项
背景 最近在用Sublime作为开发环境学习Ruby,本文就记录一下Ruby和Sublime在编码方面的问题. Sublime相关 默认的文件存储编码:UTF8 Sublime文件默认存储编码为UTF ...
- 嵌入式mp3播放器
分四部分:按键驱动,声卡驱动,Madplay播放器移植,MP3主播放器处理 按键1:播放,按键2:停止,按键3:上一曲,按键4:下一曲 UA1341内核自带声卡驱动 .解压内核: tar zxvf l ...
- matlab从文件夹名中获得该文件夹下所图像文件名
function [s,nameC]=get_FileNameFromFolderPath(path) % 函数调用:[s,nameC]=get_FileNameFromFolderPath(path ...
- 如何写科技论文How to write a technical paper
This is the evolving set of recommendations I share with my graduate students for technical writing. ...
- Python学习(六)模块
Python 模块 模块定义 随着程序越来越庞大,需要分隔成几个文件:也需要能在不同文件中复用函数.类和变量,而不是拷贝代码.为了满足这些需要,Python提供了模块. 简单来说,模块就是一个保存了P ...
- POJ 1737 Connected Graph 题解(未完成)
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- 记一个使用Client Object Model上传文件的小例子
1. 新建一个C#的Console project. 2. 给project 添加reference: Microsoft.SharePoint.Client Microsoft.SharePoint ...
- Web开发者需养成的8个好习惯
优秀的Web开发人员工作效率更高,因为他们拥有丰富的经验和良好的习惯.作者Gregor Dorfbauer分享了用于Web开发中的8个好习惯,这些良好的工作习惯不仅能提高效率,还能让您创建更加优秀的应 ...
- input type=file文件选择表单元素二三事
一.原生input上传与表单form元素 如果想使用浏览器原生特性实现文件上传(如图片)效果,父级的form元素有个东西不能丢,就是: enctype="multipart/form-dat ...
- swfupload js中 file 对象的属性
name=3cc68cfc60b87e6dd6887d8a.jpg modificationdate=Wed Apr 21 15:48:30 UTC+0800 2010 filestatus=-1 ...