Sticks POJ1011 hdu1455 (深度优先搜索DFS)

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122528 Accepted: 28390

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

题意:n根长度不同的小棍,拼成若干根长棍,要这些长棍的长度相等,并且小棍刚好都用完,问能拼成的长棍的最短长度是多少?

本题在poj accepted了,但在hdoj wa了,具体原因未知。

大致思路:先把棍子由大到小排序(贪心),计算小棍的总长度,从最长的小棍开始搜索,如果小棍的总长度能整除该长棍长度,则可能完成拼凑,深搜的边界为最坏的情况,即全部的小棍拼成一根。下面为详细代码,具体注释在代码中:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 70;
int m;
int a[maxn],used[maxn];
int aim,cnt;
bool cmp(int x,int y)
{
    return x > y;
}
bool dfs(int stick,int len,int pos) //stick表示当前组合好的棍子数,len表示已有的长度,pos表示搜索到第几根
{
    int i;
    bool sign =(len == 0?true:false);
    if(stick == cnt)    //全部完成组合,搜索成功
    {
        return true;
    }
    for(i=pos+1; i<=m; i++)
    {
        if(used[i]) continue;   //棍子已经用过了
        if(len + a[i] == aim)   //如果加起来等于目标就进行下一组的匹配
        {
            used[i] = true;
            if(dfs(stick+1,0,0))    //开始下一组的匹配
                return true;
            used[i] = false;    //若下一层搜索失败则证明第i根不应该使用
            return false;
        }
        else if(len + a[i] <aim) //长度不够
        {
            used[i] = true; //标记第i根棍子被使用
            if(dfs(stick,len+a[i],0))   //长度不够就再补,所以len + a[i]
            {
                return true;
            }
            used[i] = false;
            if(sign) return false;
            while(a[i] == a[i+1]) i++; //既然不行,那它后面跟它相同的都不行
        }
    }
    return false;
}
int main()
{
    int i,sum;
    freopen("1.in","r",stdin);
    while(cin>>m)
    {
        sum = 0;
        for(i=1; i<=m; i++)
        {
            cin>>a[i];
            sum += a[i];
        }
        sort(a+1,a+m+1,cmp);
        for(aim=a[1]; aim<=sum; aim++) //从最小的木棍长度便利到所有木棍之和的长度
        {
            if(sum % aim == 0)
            {
                cnt = sum / aim; //表示所需木棍的数量
                memset(used,false,sizeof(used));
                if(dfs(1,0,0))
                {
                    cout<<aim<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值