zoukankan      html  css  js  c++  java
  • hdu1455 dfs+剪枝

    Sticks

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


    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
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    //total能组成的木棒的组数,l:组成的木棒的长度
    int total,l;
    //num:输入的整数,sum:总长度
    int num,sum;
    typedef struct
    {
        int length;//木棒的长度
        bool mark;//木棒是否被使用过
    }Sticks;
    Sticks sticks[70];
    
    bool cmp(Sticks a,Sticks b)
    {
        return a.length>b.length;
    }
    //1 0 -1
    //s 已组成的木棒数目,len已经组成的长度,pos搜索的木棒的下标的位置
    int dfs(int s,int len,int pos)
    {
        if(s==total)
            return 1;
        for(int i=pos+1;i<num;i++){
            //如果这个木棒已经用过,则继续下一根
            if(sticks[i].mark)
                 continue;
            if(len+sticks[i].length == l)
            {
                sticks[i].mark = true;
                if(dfs(s+1,0,-1))return true;
                sticks[i].mark = false;
                return false;
            }
            else if(sticks[i].length+len<l){
                sticks[i].mark = true;
                if(dfs(s,len+sticks[i].length,i))
                return true;
                sticks[i].mark = false;
                ///剪枝:如果当前搜索时,前边的长度为0,而第一根没有成功的使用,
                ///说明第一根始终要被废弃,所以这种组合必定不会成功
                ///此处的剪枝必须有,因为这里的剪枝会节省很多的无用搜索,
                ///我试了一下,此处剪枝省去就会超时的。。。。
                if(len==0)
                     return false;
                ///剪枝:如果当前和上一次搜到的木棒是一样长的则没必要再搜一次了
                while(sticks[i].length==sticks[i+1].length)
                        i++;
            }
        }
        return false;
    }
    
    int main()
    {
    
        while(cin>>num&&num!=0)
        {
            sum = 0;//标记为0
            for(int i = 0; i < num; i++)
            {
                cin>>sticks[i].length;
                sum += sticks[i].length;
                sticks[i].mark = false;
            }
            //将木棒按照长度从长到短的顺序排序
            sort(sticks,sticks+num,cmp);
            //从木棒的最长的那根开始搜索,因为最小的组合也会大于等于最长的那根
            for(l = sticks[0].length; l <= sum; l++)
            {
                //剪枝一:如果不能被整除说明不能组成整数根木棒,搜下一个
                if(sum%l!=0)continue;
                total = sum / l;//得到木棒总数目
                if(dfs(0,0,-1))
                {
                    cout<<l<<endl;
                    break;
                }
            }
        }
        return 0;
    }
     
  • 相关阅读:
    C#实现通过拼多多分享微信公众号实现查询优惠券、佣金比率
    淘宝客常用接口整理
    京东联盟开发(1) 商品SKUID采集
    Grafana 安装及 Windows 应用程序服务配置工具 NSSM使用
    Windows Server 2008R2 配置网络负载平衡(NLB)
    IIS 日志分析工具:Log Parser Studio
    curl: (25) Failed FTP upload: 550 解决方案
    搭建TFS 2015 Build Agent环境(四)
    Dump中查看dictionary信息的方法
    Dump中查看DataTime时间方法
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/5245036.html
Copyright © 2011-2022 走看看