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): 34846 Accepted Submission(s): 15441
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.
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.
8
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
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500 int n;
int vis[maxn];
int a[maxn];
int judge(int x)
{
if(x <= ) return ;
int m = floor(sqrt(x) + 0.5);
for(int i = ; i <= m; i++)
if(x%i == ) return ;
return ;
}
void dfs(int pos, int num)
{
a[num] = pos;
if(num == n && judge(pos+))
{
for(int j = ; j <= n-; j++)
printf("%d ", a[j]);
printf("%d\n", a[n]);
return;
}
for(int i = ; i <= n; i++)
{
int sum = pos + i;
if(judge(sum) && !vis[i])
{
vis[i] = ;
dfs(i, num+);
vis[i] = ;
}
}
}
int main()
{
int cas = ;
while(~scanf("%d", &n))
{
flag = ;
memset(vis, , sizeof vis);
printf("Case %d:\n", cas++);
dfs(, );
puts("");
}
return ;
}
HDU 1016 Prime Ring Problem (回溯法)的更多相关文章
- [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(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 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(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
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 ...
- 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 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
随机推荐
- CSS开发经验
1.尽量用class来定义样式.尽量少使用 .div1 ul li{}这样的样式下去,因为如果li里面还有<div><ul><li>这些元素的话会造成干扰,应该给 ...
- cf479A Expression
A. Expression time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>
J - Islands Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Su ...
- 剑指offer-面试题16.反转链表
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的头结点 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; } 其实 ...
- 剑指offer-面试题3.二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增 的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断该数组中是否有该整数. 算法流程如下: 比如一个 ...
- 对Linux 专家非常有用的20 个命令
谢谢你你给了我们在这篇文章前两个部分的喜欢,美言和支持.在第一部分文章中我们讨论了那些都只是切换到 Linux 和linux新手所需的必要知识的用户的命令. 对 Linux 新手非常有用的 20 个命 ...
- IOS 快速排序法
- (NSMutableArray *)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSIn ...
- ACdream OJ 1153 (k-GCD)
题目链接: http://115.28.76.232/problem?pid=1153 题意: 从给定的n个数中取出k个数,使得他们的最大公约数最大,求这个最大的公约数 分析: 暴力分解不可取,我们能 ...
- H5单页面架构:requirejs + angular + angular-route
说到项目架构,往往要考虑很多方面: 方便.例如使用jquery,必然比没有使用jquery方便很多,所以大部分网站都接入类似的库: 性能优化.包括加载速度.渲染效率: 代码管理.大型项目需要考虑代码的 ...
- 全分布式环境下,DataNode不启动的问题解决
问题出现:机器重启之后,再次在master结点上面执行start-all.sh,发现有一个datanode没有启动,通过jps检查之后,发现slave1上面的datanode进程未启动 原因:每次na ...