HDU 1455 DFS(剪枝优化分析)

这是看完newpanderking写完1455题解之后,把自己的理解写下来的,很经典的dfs题

题目:

Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3582    Accepted Submission(s): 903


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
题目:有很多木棍小节,把他们组合成木棍,使最后每条木棍长度都相同,求木棍长度最小是多少

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
int a[100],vis[100],t,minlen;
int dfs(int num,int pos,int len );
bool cmp(int a,int b);
int main()
{
        while(1)
        {
            scanf("%d",&t);
            if(t==0)
                break;


            memset(vis,0,sizeof(vis));
            minlen=0;
            int i,sum=0;


            for(i=0;i<t;i++)
                {
                    scanf("%d",&a[i]);
                    sum+=a[i];
                    if(minlen<a[i])
                        minlen=a[i];
                }


            sort(a,a+t,cmp);//从大到小开始,使DFS也是从大到小开始搜索组合,
                            //否则就要从小开始累加遇到特殊数据就会耗时


            int s=sum/2+1;//如果超过了1/2的话,就无须继续深搜了,明显答案是全长
            for(;minlen<=s;minlen++)
                {
                    if(sum % minlen != 0)//如果不可以整除的话就跳过
                        continue;


                    int num=sum/minlen;


                    if( dfs(num,0,0) )
                    break;


                }
            if(minlen>s)
                printf("%d\n",sum);
            else
                printf("%d\n",minlen);
        }
    return 0;
}


int dfs(int num,int pos,int len )
{
    if(num==0)
        return 1;


    int i;
    for( i=pos;i<t;i++ )
    {
        if(vis[i])//遇到已经放入搜索组合中的节点,跳过
            continue;


        vis[i]=1;


        if(a[i]+len<minlen)//讲当前的a[i]放入搜索组合len中继续搜索新的a[i]加入
        {
            if(dfs( num,i+1,a[i]+len ))//“i+1”说明只需要遍历后面的可能,因为前面已经遍历过a+b,后面没有必要再次判断b+a
                return 1;               //继续深搜,直到遇到满足a[i]+len==minlen




            vis[i]=0;//为了主函数的下次dfs搜索,这里需要重置


            if(len==0)
                    return 0;
                    /*  len==0,说明在a[i]放入集合len中却找不到相应的元素,使len===minlen
                        也就是没有一个集合len可以放下a[i],所以失败
                        若设len==0,单纯return,则可能是错的,因为,len在放入a[i]和a[j]进去搜索不到
                        只能说明a[i]与a[j]不在同一个集合
                    */


            while(a[i]==a[i+1]) i++;//如果下一个长度与当前的长度相同,无需进行重复的搜索
        }


        else if( a[i]+len==minlen )//找到了一个组合len了,继续查找
           {
                if( dfs( num-1,0,0 ) )//全部len都找到了,返回,结束
                    return 1;


                    vis[i]=0;
                    return 0;//设一个元素a,使k1集合中其它元素之和k1.x,使a+k1.x=minlen
                            //如果搜不到足够多的集合,那么即使再找到一个集合K2,使a+k2.x=minlen
                            //还是无法搜索到足够多的集合 没必要重复
           }
        vis[i]=0;
    }
return 0;
}


bool cmp(int a,int b)
{
    return a>b;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值