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 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 x1,...,xn. 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 x1,...,xn 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 distince; the same sum connot 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 

题意:就是给定一个数(总和),然后给定n个数,让你求满足总和的所有不同的组合并输出这些组合

思路:深搜,从第0个开始深搜,当第0个的所有组合满足后,进行下一个与当前不同数的搜索,

AC代码:

#include <iostream>

#include <cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int t;

int n;

int sum;

int tag,k;

int a[13];//给定的数

int b[13];//记录满足条件的数

void dfs(int x){

int i;

if(sum>t){

return ;

}

if(sum==t){//输出

for(int j=0;j<k-1;j++){

printf("%d+",b[j]);

}

printf("%d\n",b[k-1]);

tag=1;

return;

}

for(i=x;i<n;i++){

sum+=a[i];

b[k++]=a[i];

dfs(i+1);//搜索下一个

sum-=a[i];

while(i+1<n&&a[i]==a[i+1]){ i++;}//回溯回去的时候避免搜索相同的数

k--;

}

}

int main(){

while(~scanf("%d",&t)){

scanf("%d",&n);

if(n==0){break;}

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

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

}

sum=0;

tag=0;

k=0;

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

dfs(0);

if(tag==0){

printf("NONE\n");

}

}

return 0;

}

注意;哪里必须避开搜素相同的数,深搜的想法主要感觉是回溯,回溯回去后倒地怎么来操作;

这种题看着简单,做起来也不容易,,,,菜,,,继续努力。。。。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 02:58:24

HDU 1258 Sum It Up 深搜的相关文章

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

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

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

HDU 2102 A计划 (深搜)

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位

HDU 1258 Sum It Up(dfs 巧妙去重)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7758    Accepted Submission(s): 4067 Problem Description Given a specified total t a

hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用

Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 577    Accepted Submission(s): 154 Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he

HDU 1016 Prime Ring Problem 深搜

 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Input n (0 < n

HDOJ/HDU 1242 Rescue(经典BFS深搜)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

hdu 1455 sticks(经典深搜+剪枝技巧)

 题意:有一堆的木棒,长度不一,它们是有一些整齐的木棒截断而成的,求最小的木棒原始长度. 思路很简单深搜,但是直接深搜的话会tle,首先可以对木棒长度进行排序从大到小,优先使用长度长的木棒,加入当前长度不符合,考虑下一个木棒 其次如果长度为零的时候选择木棒失败,那么直接退出,实测加上这一剪枝就可以ac,这一剪枝可以帮助我们尽可能的在靠近树根处剪枝,所以优化效果很明显. 然后是如果这次选择的木棒长度和上次失败时的一样,那么剪枝. #include<cstdio> #include<cs