hdu 1455 Sticks (dfs 经典剪枝)

Sticks

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


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
 
此题关键点:
1.按递减顺序搜索
2.剪枝:
           (1).原木棍的长度必须是所有木棍长度之和的约数
           (2).搜索原木棍长度只需搜到sum/2  若前面还没成功  答案就只能是sum了
           (3).构造一根原木棍的第一根小木棍必须是最长的   (这个剪枝好重要,第二次做也跪在这个上面了)
           (4). 2根长度相同的木棍没必要重复搜索

感想:这么多剪枝,自己第一次真的想不到,果断TLE,看来过几天得再再做一次试试。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100
using namespace std;

int n,m,ans,sum,flag;
int a[maxn],vis[maxn];

bool cmp(const int &x1,const int &x2)
{
    return x1>x2;
}
void dfs(int pos,int l,int len,int cnt)// 当前搜到的位置 + 拼一根木棍还剩的长度 + 原木棍长度 +已经拼好几根
{
    int i;
    if(cnt*len==sum)
    {
        flag=1;
        ans=len;
        return ;
    }
    if(flag) return ;
    for(i=pos;i<=n;i++)
    {
        if(!vis[i]&&a[i]<=l)
        {
            vis[i]=1;
            if(a[i]==l) dfs(1,len,len,cnt+1);
            else dfs(i+1,l-a[i],len,cnt);
            vis[i]=0;
            if(l==len) return ;        // 剪枝3:构造一根原木棍的第一根小木棍必须是最长的
            while(a[i+1]==a[i]) i++;   // 剪枝4:2根长度相同的木棍没必要重复搜索
        }
    }
}
int main()
{
    int i,j,depth;
    while(scanf("%d",&n),n)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a+1,a+n+1,cmp);            // 从大到小排序
        depth=a[1];
        memset(vis,0,sizeof(vis));
        flag=0;
        ans=sum;
        while(!flag)
        {
            if(sum%depth==0)            // 剪枝1:sum必须是depth的倍数
            {
                dfs(1,depth,depth,0);
            }
            depth++;
            if(depth>sum/2) break ;     // 剪枝2:depth>sum/2 则ans只能为sum了
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
64
40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40
40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40
40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40
40
ans:454
*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值