POJ1011 sticks DFS+剪枝

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 should 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

题意概括:有n个小木棒,问这n个小木棒最多是由几个长度相同的木棒分割得到的,输出未分割之前的长度?

解题思路:首先未分割的木棒长度肯定大于等于分割后的木棒最长的长度(max),但是不会超过所有木棒长度之和(sum)的一半,因为未分割前所有木棒长度相同。首先将n个木棒降序排列,假定未分割前的木棒长度是max,在对n个木棒进行深搜看是否可以组成几个长度相同的木棒,如果在假定长度是sum时仍未找到合适的木棒长度,这代表这n根木棒是由1根木棒分割而来的长度是sum。现在需要在深搜里面进行剪枝,因为一些操作是多余的。

    1.如果一个木棒前面的木棒和它长度相同,但是前面的没有被选中那么这个木棒继续搜索肯定不能组成假定的木棒长度。

    2.如果在回溯的时候选定这根木棒做最后一根,但是后面新的木棒无法拼成,则需要继续回溯。

    3.如果在回溯的时候选定这根木棒做第一根,但是无法拼成新的木棒,则不需要再让这根木棒做第一根进行搜索。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,flag,mark;
int cmp(int a,int b)
{
	return a>b;
}
int a[1010],book[1010],q[1010],e,L;
void DFS(int m,int l)
{
	int i;

	if(m==0&&l==0)
	{
		mark=1;
//		printf("|||||||||\n");
		return ;
	}
		
	if(l==0)
		l=L;
	for(i=0;i<n;i++)
	{
		if(book[i]==0&&a[i]<=l)
		{
			if(i>0)
			{
				if(book[i-1]==0&&a[i]==a[i-1])//如果它前面有一个数和它一样且前面的没有
					continue;				  //被选择那么它肯定不能被选择 
					
			}
			
			book[i]=1;
		//	printf("%d---%d\n",L-l,a[i]); 
			DFS(m-1,l-a[i]);
		//	printf("---------\n");
			if(mark==0)
			{
				book[i]=0;
			//	printf("%d %d %d\n",a[i],l,L);
				if(a[i]==l||l==L)//此时的l是a[i]加之前需要的长度,L是假定的木棒长度 
				{//a[i]==l代表此时的a[i]如果做最后一根,
				 //这根拼成之后后面的是无法拼成的 
				 //l==L代表此时的a[i]做第一根是无法拼成的 
				//	printf("******\n");
					return ;
				}
				
			}
		}
	}
//	printf("+++++\n");
	return ;
	
}
int main()
{
	int i,j,k,max,sum;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		memset(a,0,sizeof(a));
		max=-1;
		sum=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
			if(max<a[i])
				max=a[i];
		}
		sort(a,a+n,cmp);
//		for(i=0;i<n;i++)
//			printf("%d ",a[i]);
//		printf("\n");
		mark=0;
		for(i=max;i<=sum/2;i++)
		{
			if(sum%i!=0)
				continue;
			memset(book,0,sizeof(book));
			L=i;
		//	printf("\ni====%d\n",i);
			DFS(n,i);
			if(mark==1)
			{
				printf("%d\n",i);
				break;	
			}	
					
		}
		if(mark==0)
			printf("%d\n",sum);
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值