hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258
关键点就是一次递归里面一样的数字只能选一次。
#include <cstdio>
#include <cstring> int n,t;
int b[],c[];
bool flag;
void dfs(int k,int sum,int l)
{
if(sum==t)
{
for(int i=;i<l-;i++)
printf("%d+",c[i]);
printf("%d\n",c[l-]);
flag=;
return;
}
int last=-;
for(int i=k;i<n;i++)
{
if(sum+b[i]>t) continue;
if(b[i]!=last) //注意这个就好了
{
last=c[l]=b[i];
dfs(i+,sum+b[i],l+);
}
}
} int main()
{
// freopen("a.txt","r",stdin);
while(~scanf("%d%d",&t,&n))
{
if(n==) break;
memset(c,,sizeof(c));
flag=;
for(int i=;i<n;i++)
scanf("%d",&b[i]);
printf("Sums of %d:\n",t);
dfs(,,);
if(!flag) printf("NONE\n");
}
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1016
这题注意回溯就好。
#include <cstdio>
#include <cstring>
int n,b[];
bool used[];
bool is_prime(int x)
{
if(x==) return false;
else if(x==||x==) return true;
for(int i=;i*i<=x;i++)
if(x%i==) return false;
return true;
} void dfs(int k,int num)
{
if(num==n)
{
//printf("%d\n",num);
if(is_prime(b[n]+b[]))
{
//printf("%d\n",k);
for(int i=;i<n;i++)
printf("%d ",b[i]);
printf("%d\n",b[n]);
}
return;
}
for(int i=;i<=n;i++)
{
if(!used[i]&&is_prime(b[num]+i))
{
used[i]=true;
b[num+]=i;
//printf("%d %d\n",i,num);
dfs(i,num+);
used[i]=false;
}
}
} int main()
{
// freopen("a.txt","r",stdin);
int j=;
while(~scanf("%d",&n))
{
memset(b,,sizeof(b));
memset(used,,sizeof(used));
printf("Case %d:\n",j++);
b[]=;
used[]=true;
dfs(,);
printf("\n");
}
return ;
}
hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)的更多相关文章
- 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)
一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...
- 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(素数环问题)
传送门: 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 ...
- 杭电oj 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(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...
- Swift-4-数组和字典
// Playground - noun: a place where people can play import UIKit // 数组 字典 // 集合的可变性 赋值给var的集合是可变的mut ...
- The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near
The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...
- google大赛 入围赛250分真题
Problem StatementYou have a collection of music files with names formatted as "genre-artist-alb ...
- xmlns与targetNamespace
xmlns与targetNamespace xmlns与targetNamespacehttp://blog.sina.com.cn/weatry在使用XML Schema生成XML文件时,我们常常会 ...
- HDOJ 1062 Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service
起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...
- MVC 中 Razor 无限分类的展示
在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...
- iOS第三方支付-支付宝支付
处理手机中有无支付宝的情况支付宝调用接口: - (void)payOrder:(NSString *)orderStr fromScheme:(NSString *)schemeStr ...
- Xcode显示行号