hdu 1455 Sticks(经典深搜+剪枝)

简介:

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
这是以前做过的题poj(1011),当时从网上看了好久,虽然过了,可是隔了这么久了,发现还是有必要再写一遍
这次AC的就容易多了,和以前写的差不多,这次用了一个结构体存储木棒信息,但是所有的剪枝还是没有少,这里是剪枝的经典运用,一定要学好了。。。。
干巴爹。。。
复制代码
#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;
}
//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(1,0,-1))
            {
                cout<<l<<endl;
                break;
            }
        }
    }
    return 0;
}
复制代码

 










本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/10/08/2715413.html ,如需转载请自行联系原作者


相关文章
|
算法
Bellman-Ford算法求最短路和负环
Bellman-Ford算法求最短路和负环
97 0
Bellman-Ford算法求最短路和负环
|
算法
图论最短路及生成树(Prim,Djikstra,Spfa,Bellan-ford,kruskal,topsort)
图论最短路及生成树(Prim,Djikstra,Spfa,Bellan-ford,kruskal,topsort)
149 1
[Nowcoder] network | Tarjan 边双连通分量 | 缩点 | LCA倍增优化 | 并查集
题目描述 A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers.
134 0
[Nowcoder] network | Tarjan 边双连通分量 | 缩点 | LCA倍增优化 | 并查集
Optimization for UltraNet二分最小生成树
二分边,要把边最小值尽可能最大化,可以对这个值进行二分判断是否可以,在判断的过程中,如果是要连接的次数等于n-1,n为点的数量,点之间如果要构成生成树最少连接的数量为n-1,所以说判断的时候可以通过连接的次数来判断是否可以构成生成树 将最小生成树的那条边进行最小值的最大化之后,就可以再往后遍历的过程中,把要用到的n-1条边进行记录下来,然后进行下一步操作->计算边权 将要用到的边记录下来之后,按照边权的大小对他进行从大到小进行排序,用并查集来维护两个联通块的大小,这个联通块对答案的贡献就是两个联通块的大小size_a * size_b * w
153 0
Optimization for UltraNet二分最小生成树
【HDU】1175 连连看(BFS + 剪枝)
【HDU】1175 连连看(BFS + 剪枝)
278 0
【HDU】1175 连连看(BFS + 剪枝)
|
C语言
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
108 0
Sticks(剪枝+BFS)
Problem Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long.
1423 0