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;
    }
     
  • 相关阅读:
    启动hadoop集群的时候只能启动一个namenode,另一个报错There appears to be a gap in the edit log. We expected txid 6, but got txid 10.
    YARN的HA
    CSS3 文本效果:text-shadow、box-shadow、text-overflow、word-wrap、word-break
    CSS3 渐变(Gradients):在两个或多个指定的颜色之间显示平稳的过渡
    CSS3 背景:几个新的背景属性,提供更大背景元素控制
    CSS3 圆角:使用 CSS3 border-radius 属性,你可以给任何元素制作 "圆角"
    CSS3 边框:创建圆角边框,添加阴影框
    CSS 网页布局的集中实现方式
    CSS 计数器:通过一个变量来设置,根据规则递增变量
    CSS 表单:使用 CSS 来渲染 HTML 的表单元素
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/5245036.html
Copyright © 2011-2022 走看看