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): 34609 Accepted Submission(s): 15327
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.
这道题就是回溯暴力。 首先打出一个素数表, 然后DFS回溯判断即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int is_prime(int x)
{
for(int i=; i*i<=x; i++)
if(x%i==) return ;
return ;
} int n, A[];
bool isp[], vis[];
void dfs(int cur)
{
if(cur==n&&isp[A[]+A[n-]])
{
for(int i=; i<n-; i++)
{
printf("%d ", A[i]);
}
printf("%d\n", A[n-]);
}
else for(int i=; i<=n; i++)
if(!vis[i]&&isp[i+A[cur-]])
{
A[cur] = i;
vis[i] = ;
dfs(cur+);
vis[i]=;
}
} int main()
{
int kase = ;
while(scanf("%d", &n)!=EOF)
{
printf("Case %d:\n", ++kase);
for(int i=; i<=n*; i++)
isp[i] = is_prime(i);
memset(vis, , sizeof(vis));
A[] = ;
dfs();
printf("\n");
}
return ;
}
HDU1016 Prime Ring Problem(DFS回溯)的更多相关文章
- 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 ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu1016 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 ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
随机推荐
- Python 进阶(五)定制类
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAugAAAF/CAIAAACUs6uhAAAgAElEQVR4nOzdZXubx7ov8PPV9tlrt0 ...
- Environment中针对的读写权限判断
Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态,并且需要在AndroidManifes ...
- Redis 转
Redis 简介 Redis实践 Redis命令总结
- Pascal's Triangle
class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector< ...
- struts2多线程数据乱窜问题
转自:http://love398146779.iteye.com/blog/1781680 1.struts2为每个线程提供一个action实例,多线程访问时不会出现问题.当使用spring管理st ...
- 在MyEclipse中搭建Spring MVC开发环境
环境版本 IDE:MyEclipse 8.5 Spring:spring 3.2.8 JDK:1.6 1.打开MyEclipse-->File-->New-->Web Project ...
- listview实现点击条目上的箭头展开隐藏菜单。
效果如下图,当点击listview中的小三角时,显示出下面布局,再点隐藏, 点击其他条目的三角时,上一个展开的条目隐藏的同时展开当前条目. 思路是在item布局中放入展开菜单的布局,并设置状态为隐藏, ...
- ACM题目————STL练习之Ananagrams
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- php之上传类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/5/26 * Time: 20:29 */ class u ...
- easyui datebox 只选择年月
//将日期输入框变为年月的函数方法 var month=0; $('#effectiveDate').datebox({ onShowPanel: function () ...