深度优先搜索(DFS,Depth-First Search)是搜索手段之一。它从某个状态开始,不断的转移状态知道无法转移,然后退回到前一步的状态,继续转移到其他状态,如此不断重复,直到找到最终的解。

问题给定整数a1,a2...an,判断是否可以从中选出若干数,使它们的和恰好为k。

1<=n<=20

-1e8<=ai<=10e8

-1e8<=k<=1e8

输入:

n=4 k=13

a={1 2 4 7}

输出:

Yes (13 = 2 + 4 + 7)

输入:

n=4 k=15

a={1 2 4 7}

输出:

No

枚举每一种情况复杂度为O(2^n)。利用dfs搜索可以枚举每一种情况代码如下:

#include <iostream>
#include<stack>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define pi acos(-1)
const int N=1e5;
int n,a[N],ans[N],k;
int h=0;
bool dfs(int i,int sum)//i代表第i个数(从0开始)
{
if(i==n) return sum==k;
if(dfs(i+1,sum+a[i])) //加入a[i]往下递归
{
ans[h++]=a[i];
return true;
}
if(dfs(i+1,sum))//不加a[i]往下递归  可以看成是加不加a[i]的问题(包括了所有状态)
{
return true;
}
return false;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&k)!=EOF)
{
h=0;
for(i=0; i<n; i++)
scanf("%d",&a[i]);
if(dfs(0,0))
{
printf("Yes (%d =",k);
for(i=0; i<h; i++)
{
printf(" %d",ans[i]);
if(i!=h-1)
printf(" +");
}
printf(")\n");
}
else
printf("No\n");
}
return 0;
}

  

dfs枚举的更多相关文章

  1. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  2. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  3. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  4. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  7. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  8. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  9. The Pilots Brothers' refrigerator DFS+枚举

    Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...

  10. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

随机推荐

  1. [Docker]学习笔记--简单介绍

    学习docker已经有一段时间了,一直没有静下心来好好总结一下. 最近用docker搭了一整套Gitlab的持续集成环境.(会在下一篇中详细的讲解具体步骤,敬请期待) 感觉是时候写点东西和大家一起分享 ...

  2. web容器 web服务器 servlet/jsp容器 之间的区别和关系是什么?

    web容器 web服务器 servlet/jsp容器 之间的区别和关系是什么? 这是我在网上找的一些资料:1. Web浏览器除了可以在本地硬盘上打开网页文档外,还可以使用http网络协议从网络上的We ...

  3. xampp怎么操作数据库mysql

    1.打开软件的主界面,打开Apache和MySQL,然后点击MySQL后面的admin.且我操作时,Apache,MySQL要启动,才打的开. 2.打开MySQL,报错. 09:00:23 [mysq ...

  4. UI通过UISlider编写游戏第六感

    #import "RootViewController.h" @interface RootViewController (){    UILabel *scoreLabel; } ...

  5. dev 小问题列表

    1. MemoEdit > Lines Text lines are separated by line feed and carriage return characters ("\ ...

  6. js验证表单大全2

    屏蔽右键 很酷  oncontextmenu="return false" ondragstart="return false"onselectstart=&q ...

  7. Python基础-文件的基本操作

    测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...

  8. Oracle 11G无法导出空表的解决办法

    11G中有个新特性,当表无数据时,不分配segment,以节省空间解决方法:1.insert一行,再rollback就产生segment了.该方法是在在空表中插入数据,再删除,则产生segment.导 ...

  9. 源码编译搭建LAMP

    环境版本信息: RHEL 5.3 Apache / 2.4.16 PHP / 5.4.45 mysql-5.5.45 源代码编译 安装方式 1: configure 配置 以及定制我们的软件包 2: ...

  10. Windows.old

    如果通过执行自定义安装来安装 Windows 7,而没有在安装过程中格式化分区,则以前版本的 Windows中使用的文件存储在 Windows.old文件夹中.此文件夹中文件的类型取决于您的电脑.使用 ...