hdu 1258 深度优先搜索+步长+序列和+输出+虚拟节点+文件操作
2015-12-02 13:43
295 查看
题目链接: 点击打开链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5473 Accepted Submission(s): 2862
Problem 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
分析:深度优先搜索各节点,建立一个虚拟节点0,将n棵树的森林连成一棵树,最重要的是 去重 ,方法是,对一个节点搜索完毕后,若下一个节点与当前节点相同,则跳过直至找到一个不同节点。
代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<stack>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
# define maxn 1005
int a[maxn] ;
int chose[maxn] ;
int visited[maxn] ;
int t , n ,flag;
void dfs(int v ,int sum , int step ){
//printf("chose vex = %d ,\n" , v ) ;
if(sum > t)
return ;
if(sum == t){
flag = 1 ;
for(int i = 1 ; i<= step ; i++){
if(i == 1)
printf("%d" , chose[i]) ;
else
printf("+%d" , chose[i]) ;
}
cout<< endl;
return ;
}
for(int i =1 ; i <= n ; i++){
if(!visited[i] && i> v && sum + a[i] <= t){
visited[i] = 1 ;
chose[step + 1 ] = a[i] ;
dfs(i , sum + a[i] , step + 1) ;
//printf("return i= %d\n", i) ;
visited[i] = 0 ;
while(i+1 <= n && a[i+1] == a[i]) //搜索完毕后,若下一个搜索数与当前相同,则跳过直至不同
i++ ;
}
}
}
int main(){
// FILE * fp ;
// fp = fopen("in.txt" , "r") ;
while(scanf("%d%d" , &t,&n) != EOF){
if(n == 0){
break ;
}
flag = 0 ;
for(int i =1 ; i<= n ;i++)
scanf("%d" , &a[i]) ;
//fscanf(fp , "%d" , &a[i]) ;
memset(visited , 0 , sizeof(visited)) ;
printf("Sums of %d:\n" , t ) ;
dfs(0 , 0 , 0) ; //引入虚拟节点0,将n个森林连成一棵树
if(!flag)
printf("NONE\n") ;
}
//fclose(fp) ;
return 0 ;
}
Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5473 Accepted Submission(s): 2862
Problem 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
分析:深度优先搜索各节点,建立一个虚拟节点0,将n棵树的森林连成一棵树,最重要的是 去重 ,方法是,对一个节点搜索完毕后,若下一个节点与当前节点相同,则跳过直至找到一个不同节点。
代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<stack>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
# define maxn 1005
int a[maxn] ;
int chose[maxn] ;
int visited[maxn] ;
int t , n ,flag;
void dfs(int v ,int sum , int step ){
//printf("chose vex = %d ,\n" , v ) ;
if(sum > t)
return ;
if(sum == t){
flag = 1 ;
for(int i = 1 ; i<= step ; i++){
if(i == 1)
printf("%d" , chose[i]) ;
else
printf("+%d" , chose[i]) ;
}
cout<< endl;
return ;
}
for(int i =1 ; i <= n ; i++){
if(!visited[i] && i> v && sum + a[i] <= t){
visited[i] = 1 ;
chose[step + 1 ] = a[i] ;
dfs(i , sum + a[i] , step + 1) ;
//printf("return i= %d\n", i) ;
visited[i] = 0 ;
while(i+1 <= n && a[i+1] == a[i]) //搜索完毕后,若下一个搜索数与当前相同,则跳过直至不同
i++ ;
}
}
}
int main(){
// FILE * fp ;
// fp = fopen("in.txt" , "r") ;
while(scanf("%d%d" , &t,&n) != EOF){
if(n == 0){
break ;
}
flag = 0 ;
for(int i =1 ; i<= n ;i++)
scanf("%d" , &a[i]) ;
//fscanf(fp , "%d" , &a[i]) ;
memset(visited , 0 , sizeof(visited)) ;
printf("Sums of %d:\n" , t ) ;
dfs(0 , 0 , 0) ; //引入虚拟节点0,将n个森林连成一棵树
if(!flag)
printf("NONE\n") ;
}
//fclose(fp) ;
return 0 ;
}
相关文章推荐
- C 语言实现迷宫 DFS算法
- 图的基本算法
- 程序设计实习MOOC/第十五周编程作业/A:棋盘问题
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
- hdu 2614 Beat
- hdu 1501 Zipper 拉链
- hdu 1175 连连看 经典dfs
- Oil Deposits(深度或广度搜索)
- HDU 4536 XCOM Enemy Unknown
- HDU 1241 Oil Deposits
- HDU 1518 Square
- HDU 1016 Prime Ring Problem
- HDU 1312 Red and Black
- POJ1321,深度优先搜索
- 深度优先搜索
- 【Jason's_Knowledge】算法竞赛中的递归与迭代及其延伸内容简述
- 用试探回溯法解决N皇后问题
- hdu 1241 田字格八个方向的深度优先搜索 求连通分量个数
- hdu 2952 田字格四个方向的深度优先搜索 计算连通分量
- POJ 3009