搜索(DFS)

Sum It Up










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5684   Accepted: 2894

Description

Given a specified total t and a list of n integers,
find all distinct sums using numbers from the list that add up to t. For
example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are
four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used
within a sum as many times as it appears in the list, and a single number counts
as a sum.) Your job is to solve this problem in general.

Input

The input will contain one or more test cases, one per
line. Each test case contains t, the total, followed by n, the number of
integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it
signals the end of the input; otherwise, t will be a positive integer less than
1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n
will be positive integers less than 100. All numbers will be separated by
exactly one space. The numbers in each list appear in nonincreasing order, and
there may be repetitions.

Output

For each test case, first output a line containing
`Sums of‘, the total, and a colon. Then output each sum, one per line; if there
are no sums, output the line `NONE‘. The numbers within each sum must appear in
nonincreasing order. A number may be repeated in the sum as many times as it was
repeated in the original list. The sums themselves must be sorted in decreasing
order based on the numbers appearing in the sum. In other words, the sums must
be sorted by their first number; sums with the same first number must be sorted
by their second number; sums with the same first two numbers must be sorted by
their third number; and so on. Within each test case, all sums must be distinct;
the same sum cannot appear twice.

Sample Input

4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0

Sample Output

Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <algorithm>

using
namespace std;

const
int maxn=10+5;

int
a[maxn],cur[maxn];

int
ok,sum,n;

inline
void DFS(int
index,int
cursum,int
cnt)

{

    if(cursum==0)

    {

        ok=0;

        printf("%d",cur[0]);

        for(int
i=1;i<cnt;i++)

        {

            printf("+%d",cur[i]);

        }

        printf("\n");

        return
;

    }

    else

    {

        for(int
i=index;i<n;i++)

        {

            if(i==index||a[i]!=a[i-1])      //注意去重,如果当前这个数是这一层的第一个,一定要,如果不是第一个,并且和前一个相同,就不用考虑

            {                  //因为如果与前面一个相同,就相当于当前这个串是前面的一个子串

                cur[cnt]=a[i];

                DFS(i+1,cursum-a[i],cnt+1);

            }

        }

    }

    return
;

}

int
main()

{

    while(scanf("%d%d",&sum,&n)!=EOF&&n!=0)

    {

        memset(cur,0,sizeof(cur));

        memset(a,0,sizeof(a));

        for(int
i=0;i<n;i++)

        scanf("%d",&a[i]);

        ok=1;

        printf("Sums of %d:\n",sum);

        DFS(0,sum,0);

        if(ok)

        printf("NONE\n");

    }

    return
0;

}

时间: 2024-07-31 05:18:37

搜索(DFS)的相关文章

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

[ACM] poj 1088 滑雪 (记忆化搜索DFS)

求n*m网格内矩形的数目[ACM] poj 1088 滑雪 (记忆化搜索DFS),布布扣,bubuko.com

pku 2488 A Knight&#39;s Journey (搜索 DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 9822 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey aro

[LeetCode OJ] Word Search 深度优先搜索DFS

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

题目传送门 1 /* 2 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 3 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑 4 考虑三种情况:a[]连续两个颜色相同,输出11:b[]连续两个相同,输出22: 5 a[x] != b[y], 输出12:否则Impossible 6 详细解释:http://blog.csdn.net/jsun_moon/article/details/10254

HDU 2553 N皇后问题 (搜索DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7743    Accepted Submission(s): 3481 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

深度优先搜索(DFS)详解

深度优先搜索(DFS) [算法入门] 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念. 你可以跳过第二节先看第三节,:) 2.深度优先搜索VS广度优先搜索 2.1演示深度优先搜索的过程 还是引用上篇文章的样例图,起点仍然是V0,我们修改一下题目意思,只需要让你找出一条V0到V6的道路,而无需

朴素搜索dfs, 简单的剪枝搜索

为做一个项目选择最合适的语言固然重要,但是,掌握一门自己熟练的兵器,也很重要. ===================================================================================================================== 继续总结搜索类的题目,这一类的题目,目前仅分析简单粗暴的dfs搜索,以及简单的剪枝. 参考的题目: http://acm.hdu.edu.cn/showproblem.php?pid=

搜索 || DFS || POJ 2488 A Knight&#39;s Journey

给一个矩形棋盘,每次走日字,问能否不重复的走完棋盘的每个点,并将路径按字典序输出 *解法:按字典序输出路径,因此方向向量的数组按字典序写顺序,dfs+回溯,注意flag退出递归的判断,并且用pre记录路径 #include <iostream> #include <cstdio> #include <cstring> using namespace std; char a[30][30]; int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};