DFS深度优先搜索(6)--hdu1455(经典深搜+剪枝)

                                                           Sticks

                                                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



Problem Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
 

Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
 

Output
The output file contains the smallest possible length of original sticks, one per line.
 

Sample Input

9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
 

Sample Output

6 5
 

Source
ACM暑期集训队练习赛(七)


题意:有一堆的木棒,长度不一,它们是有一些整齐的木棒截断而成的,求最小的木棒原始长度。
思路很简单,深搜,但是直接深搜的话会tle,首先贪心一发,可以对木棒长度进行排序从大到小,优先使用长度长的木棒,加入当前长度不符合,考虑下一个木棒。然后就是几个剪枝;具体见代码注释:
#include 
#include 
#include 
using namespace std;
int n;
int a[65];
int vis[65];
int flag;
bool cmp(int a,int b)
{
	return a>b;
}
void Dfs(int lim,int sum,int cnt)   //lim指当前枚举的木棒长度   
{                                   //sum指拼凑出来的木棒长度  超过lim则减去lim 
	if(flag)return;                 //cnt指当前一共用了多少木棍 
	if(sum>lim) return;
	if(sum==lim&&cnt==n)
	{
		flag=1;
		return;
	}
	if(sum==lim)sum=0;
	int i;
	for(i=0;i





 

你可能感兴趣的:(【DFS(深度优先搜索)】)