深度优先搜索(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. BAPI、badi 和 LSMW 的相同点和不同点及具体操作

    一开始badi.BAPI.LSMW关系极其混乱,好像都是传输数据, just transfer data from SAP system to non-SAP system or transfer d ...

  2. 微信小程序结构分析

    1.Console页面:控制台信息页两个作用: (1)开发直接输入代码并且进行调试: (2)显示小程序的错误输出: 2.Sources页面:源文件调试信息页,用于显示当前项目的脚本文件. 3.Netw ...

  3. Mac下通过shell脚本修改properties文件

    通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...

  4. [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作

    插入操作: 直接给个例子 import static org.springframework.data.mongodb.core.query.Criteria.where; import static ...

  5. 1052 最大M子段和(DP)

    1052 最大M子段和 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个整数组成的序列a[1],a[2],a[3],…,a[n],将这N个数划分为互不相交的M个 ...

  6. E - Rails (栈)

    E - Rails Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description The ...

  7. MVC5学习系列

    前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...

  8. Java的默认构造函数调用

    // 注意,这里不能是 public class OOO,否则编译无法通过,需把文件命名成 OOO.java class OOO { // 注意:如果不定义OOO(),那么Shapes(int i)编 ...

  9. EChars文档

    http://echarts.baidu.com/echarts2/doc/doc.html#SeriesMap http://echarts.baidu.com/option.html

  10. python 获取exception 名字

    def func(): list = [] usr = input('username:') pwd = input('password:') try: list[4] # 这个是调用不了的,因为列表 ...