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

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

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/81104138