Sum It Up -- 深搜 ---较难

每一行都是一组测试案例   第一个数字 表示总和 第二个数字表示 一共有几个可用数据  现在 按照从小到大的顺序   输出  那些数字中若干数字之和为总和的  信息 /.

很好很明显的  遍历痕迹 , 多看多练

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 using namespace std;   //  输入的时候  就是 有序的 .
15 int a[1005],flag,n,m,vis[1005],res[1005]={1005};
16 void DFS(int m,int cnt)
17 {
18     int i;
19     if(m==0)    // 攒够了之后
20     {
21         flag=1;
22         for(int j=1;j<cnt-1;j++)
23             printf("%d+",res[j]);
24         printf("%d\n",res[cnt-1]);
25         return ;
26     }
27     for(int i=1;i<=n;i++)
28     {
29         if(!vis[i]&&m-a[i]>=0&&a[i]<=res[cnt-1])  //
30         {
31             vis[i]=1;
32             res[cnt]=a[i];
33             DFS(m-a[i],cnt+1);
34             vis[i]=0;
35             while(a[i]==a[i+1]&&i<=n)
36                 ++i;
37         }
38     }
39 }
40 int main()
41 {
42     while(scanf("%d%d",&m,&n),(n||m))
43     {
44         for(int i=1;i<=n;i++)
45             scanf("%d",&a[i]);
46         flag=0;
47         printf("Sums of %d:\n",m);
48         memset(vis,0,sizeof(vis));
49         DFS(m,1);
50         if(!flag)
51             printf("NONE\n");
52     }
53     return 0;
54 }
时间: 2024-08-02 22:10:47

Sum It Up -- 深搜 ---较难的相关文章

HDU 1258 Sum It Up 深搜

 Crawling in process... Crawling failed   Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1258 Description Given a specified total t and a list of n integers, find all distinct sums using numb

NYoj The partial sum problem(简单深搜+优化)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8

简单深搜:POJ1546——Sum it up

结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 可以尝试去画一下二叉树理解一下,查看遍历的路径 代码还是百度到别人的自己再参悟- -佩服别人的功底啊 先上代码: /*POJ 1546 Sum it up*/ # include<iostream> # include<algorithm> # include<cstdio&g

水叮当的舞步 深搜

背景 Background 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变. 为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ 描述 Description 地毯上的格子有N行N列,每个格子用一个0~5之间的数字代表它的颜色. 水叮当可以随意选择一个0~5之间的颜色,然后轻轻地跳动一步,左上角的格子所在的联通块里的所有格子就会变成她选择的那种颜色.这里连通定义为:两个格子有公共边,并且颜色相同. 由于水叮当是施展轻功来跳舞的,

深搜笔记

看搜索已经很久了,对于搜索的思想从原来的死记硬背到现在终于懂了一点其实也蛮不错吧,我自己先总结出来了几条关于在图里面深搜的几条方法,仅供参考: 首先:我们得知道深搜是什么,其次于广搜的区别是什么.然后又哪些模板 举一个深搜例子:red and black:这是初学者最常见到的题.对于这题我们所要考虑的就是一个'.'的个数,这个题先要找到@的位置,这个好办,直接: for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(map[i][j]=='@') }

HDU5723 Abandoned country 最小生成树+深搜回溯法

Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guarante

hdu1518(Square)深搜+剪枝

点击打开杭电1518 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20,

杭电 1518 Square (深搜)(回溯)

http://acm.hdu.edu.cn/showproblem.php?pid=1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8343    Accepted Submission(s): 2706 Problem Description Given a set of sticks of various

2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there are n cities and (n−1) (bidirectional) roads between cities. The i-th road is between the ai-th and bi-th cities. It is guaranteed that cities are conne