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 ...
随机推荐
- java 验证日期
- C++实现CString和string的互相转换
CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); u ...
- WPF 详解模板
在WPF中有三大模板 ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和 ItemsPanelTemplate是控件模 ...
- redis cluster安装部署(测试环境)
redis 应用于web前端,做缓存和数据存取的速度是挺可观的,最近看了一些资料,手痒了,就弄了一个测试环境,两台方案,试用一下. ##Redis 集群部署## 一,方案调研: 参考博客: http: ...
- Sqli-labs less 61
Less-61 此处对于id处理还是有点奇葩的,第一次遇到利用两层括号的.(可能我头发比较长,见识短了).形式和上述是一样的 payload: http://127.0.0.1/sqli-labs/L ...
- ASP.NET Web.Config配置数据库连接的一种方法
所谓的webConfig配置数据库连接就是在里面某个特定名称的节点中写下ADP.NET中的ConnectString,就这么简单 1.首先在Web.Config文件里写下数据库连接字符串. <c ...
- POJ 3185
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4088 Accepted: 1609 D ...
- UITableView多选删除
设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...
- POJ 1650
#include <iostream> #include <cmath> //wo de 编译器里的这个abs的功能不能用啊! using namespace std; int ...
- HDU 1789 Doing Homework again (贪心)
Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...