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 ...
随机推荐
- security with restful
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- JAVA常见算法题(三十一)---冒泡排序
package com.jege.spring.boot.hello.world; /** * java算法之冒泡排序<br> * 将数组按照从大到小的顺序排列<br> * * ...
- input 输入框默认获得焦点
JavaScript实现默认焦点: 如下写<body>标签: <body onload="window.formLogin.user.focus()"> & ...
- SQL PRIMARY KEY,SQL FOREIGN KEY
A primary key is defined as a column or a group of column that their value are always be unique. Nor ...
- jdk1.7下载路径
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
- [ACM] POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23093 Accepted: ...
- [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN
Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...
- 《iOS用户体验》总结与思考-改动版
假设转载此文.请注明出处:http://blog.csdn.net/paulery2012/article/details/25157347,谢谢. 前言: 本文是在阅读<ios用户体验> ...
- ubuntu12.04下helloworld驱动从失败到成功过程
最近在看linux的设备驱动程序,写一个简单的helloworld程序都花了我好久的时间,具体过程如下: 编写helloworld.c 编写Makefile 注意,makefile中的命令那里是一个t ...
- Vue 源码 基础知识点
1.数据类型判断 const _toString = Object.prototype.toString function toRawType(value) { return _toString.ca ...