POJ 1011 Sticks(很经典的DFS+减枝)

针对经典算法问题Sticks,本文详细解析了其背景与要求,提供了两种不同思路的代码实现,一种为错误示范,另一种则为经过三次减枝优化后的高效解决方案,并附带测试数据。

Sticks

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 153856 Accepted: 36719

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

题目大意: 给定一些小木棍,每根木棍的长度不超过50,这些小木棍是通过几根相同长度的小木棍随意裁剪得到的,现在请编程求出原始木棍的最短长度。

做题历程:好清晰的思路,DFS嘛最多减下枝嘛.......实在是年少轻狂啊.......数个小时后,才认识到这是一道不一般的搜索。1000ms限时?提交15w次?!,,,,实在是太年轻,但写了又舍不得放弃,感觉就差那么一点点。这题不仅仅是减一下枝,在我的锲而不舍的精神下,成功减了2次枝,哇,感觉自己好厉害,提交一下,,,呵呵,还是超时........

思路:我的想法是求最小棍长,那就枚举棍子的最小长度,将棍子长度排序,然后就该长度进行搜索。此处第一次减枝(满足长度能被总长度整除).然后,因为是多次遍历,我是按照一根一根的找,如果第一根都找不到那后面的肯定也找不到(二次减枝)。结果还是超时。

正确的是还要继续减枝,不应该向我一样一原根一原根的搜索,应该一截一截的搜索。(这大概就是限制我减枝的原因吧)

一截一截的搜索,在搜索时可以利用从大到小搜,大的被取后,只能取比他小的,所以在枚举时复杂度减小。

我的错误入手点代码(题还是做的太少,太年轻)(下面提供的恶心数据中4组超时):

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
int p[100],vis[100]={0},n,fag=0,z;
void dfs(int x,int y,int t)
{
	int pig=0;
	 if(x==0&&y>1&&fag==0)//第一根找到,找下一根 
	 {
	 	dfs(z,y-1,t);
	 }
	 if(x==0&&y==1)//全部找完 
	 {	
	    for(int i=0;i<n;i++)
	 	{
	 		if(vis[i]==0) 
			{
				pig=1;return;//有棍没选
			} 
		}
		if(pig==0)
	 	{
		   fag=1;return;//找到 
	    }
	 }
	 for(int i=n-1;i>=0;i--)//先从大的开始
	 {
	 	if(vis[i]==0&&x>=p[i])
	 	{
	 		vis[i]=1;
	 		dfs(x-p[i],y,i-1);
	 		vis[i]=0;
		}
	 }
	 return;//第一根找不到 
}
int main()
{
	int i,j;
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		fag=0; 
		memset(p,0,sizeof(p));
		int s=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&p[i]);
			s=s+p[i];
		}
		sort(p,p+n);
		for(j=p[n-1];j<=s/2;j++)//枚举最小长度 
		{
			if(s%j==0)
			{
				z=j; 
			    memset(vis,0,sizeof(vis));
			    dfs(j,s/j,n-1);//x为最小长度,y为原棍的数量 
				if(fag!=0)
				{
					printf("%d\n",j);
					break;
				}
			}
		}
		if(fag==0)
		{
			printf("%d\n",s);
		}
	}
	return 0;
} 

正确的三次减枝代码:(入手点错了,实在想不出来参考了大佬的做法)

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int cmp(const int a,const int b){
	return a>b;
}
int n,len;
int p[65],vis[65];
int DFS(int x,int y,int s)
{
	int i,j;
	if(y==0) //长度为0时(能取出满足该长度的棍子时)3次减枝 
	{
		s=s-len;//总长度减去一根的长度 
		if(s==0) return 1;//满足条件 
		for(i=0;vis[i]!=0;i++);  //枚举还没有取的棍子 
		{
		   vis[i]=1; //标记已取 
		   if(DFS(i+1,len-p[i],s)) return 1; //搜索 
		   s=s+len; 
		   vis[i]=0;//返回该次搜索前状态 
	    }
		return 0;  
	}
	else 
	{
		for(i=x;i<n;i++) //枚举第x根及以后的棍子 
		{
			if(i>0&&p[i]==p[i-1]&&vis[i-1]==0) //与该根相同的上一根(上一根没用上,这一根自然也用不上)2次减枝 
			{
				continue;
			} 
			if(p[i]<=y&&vis[i]==0) //能取该棍子 
			{
				vis[i]=1;
				y=y-p[i];
				if(DFS(i+1,y,s)==1) return 1;//更新x的值,因为棍子按照长短排序,长的取后只能取短的,所以前面的不用再遍历(1次减枝) 
				vis[i]=0;
				y=y+p[i]; //回归状态 
			}
		 } 
		 return 0; 
	}
}
int main()
{
	int i,j,s=0,fag;
	while(~scanf("%d",&n)&&n!=0)
	{
		s=0;
		fag=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&p[i]);
			s=s+p[i];
		}
		sort(p,p+n,cmp);
		for(len=p[0];len<=s/2;len++)//枚举长度 
		{
			memset(vis,0,sizeof(vis));
			if(s%len==0&&DFS(0,len,s)==1)//能整除且满足条件 (1次减枝) 
			{
				fag=1;
				printf("%d\n",len);
				break;
			}
		}
		if(fag==0) 
		printf("%d\n",s);
	}
	return 0;
}

另外提供几组数据(恶心):(此数据取自POJ评论区,出自万千前辈之手)

9      15 3 2 11 4 1 8 8 8

6       6 2 2 4 8 8

5       1 1 1 1 1

2       1 1

4       2 2 9 9

3       1 2 3

64     40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40            40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40

7       49 48 47 46 45 44 43

7       3 4 5 5 5 5 13

7       2 7 7 7 7 10 20

6       1 2 3 11 11 20

7       63 2 44 12 60 35 60

9       5 2 1 5 2 1 5 2 1

4       1 2 3 4

64     32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32          32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 33 33 31 31

64    40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40           40 40 40 40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40 40

45    15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8

0

答案:20 10 1 1 11 3 1251 322 20 30 24 276 6 5 64 454 20

POJ 1011 是“Sticks”问题,主要是将一些长度不一的小木棍拼接成若干根等长的大木棍,求大木棍的最小可能长度。该问题通常使用深度优先搜索(DFS)结合剪策略来解决。 由于使用了剪策略,其时间复杂度难以精确分析,因为剪少实际搜索的状态数。在最坏情况下,没有任何剪生效,搜索空间为指数级。假设木棍数量为 $n$,木棍长度的上限为 $m$,不剪DFS 的时间复杂度近似为 $O(2^n)$,因为对于每根木棍都有两种选择:放入当前大木棍或者不放入。 以下是一个简单的 Python 代码示例,实现了该问题的基本思路: ```python def dfs(sticks, target, current_length, start_index, used): if all(used): return True if current_length == target: return dfs(sticks, target, 0, 0, used) last_failed = -1 for i in range(start_index, len(sticks)): if not used[i] and sticks[i] != last_failed and current_length + sticks[i] <= target: used[i] = True if dfs(sticks, target, current_length + sticks[i], i + 1, used): return True used[i] = False last_failed = sticks[i] if current_length == 0 or current_length + sticks[i] == target: return False return False def solve(sticks): total_length = sum(sticks) max_length = max(sticks) sticks.sort(reverse=True) for target in range(max_length, total_length + 1): if total_length % target == 0: used = [False] * len(sticks) if dfs(sticks, target, 0, 0, used): return target sticks = [2, 2, 2, 2] print(solve(sticks)) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值