POJ1011 Sticks dfs+剪枝

该博客介绍了如何使用深度优先搜索(DFS)结合剪枝策略来解决POJ1011 Sticks问题。通过实例解析输入输出格式,并提供样例输入及对应的预期输出,帮助读者理解算法的应用。
黑书上面的题目总是这么折磨人。。寒假过去十多天了。整个人都水了。到最后才发现原来这道题目还有中文版的。。。。好神奇。
这道剪枝真的很经典。寒假在家能看看这种题目效果还是很不错的。
这道题目是深搜+各种剪枝。  暴力的dfs铁定超时。
 
下面给出我程序中所使用的剪枝。
1:如果我现在要拼凑第i根原始木棒,我们指定一根木棒为第一个木棒,如果该木棒与剩下的木棒无论怎么组合都无法拼出原始木棒的长度,那么直接回溯到拼凑第i-1根木棒,重新枚举第i-1根原始木棒的拼凑策略。因为最后我们一定要用光所有木棒,那么这根木棒我们最后还是会用到,但此时已经无法通过剩余木棒拼凑出目标长度的原始木棒,故需要剪枝。
2:木棒从大到小排序,如果我们用长度为l的木棒拼凑原始木棒失败,则跳过接下来所有与该木棒相同的木棒。这样可以减去很多分支。
3:先将所有木棒从大到小排序。从长的木棒开始搜索这样我们:便可以在靠近根部的位置进行剪枝,可以增加剪枝效率。
4:原始木棒的长度范围 [最长的木棒长度,所有木棒的长度和].
 
代码效率不高 16ms 感觉还是有点问题,应该可以再改进
Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 104326 Accepted: 23763

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

int s[70];
int n,m,sum,l,len;
bool vis[70];
bool flag;

bool cmp(int a,int b)
{
    return a>b;
}
inline int max(int a,int b)
{
    return a>b?a:b;
}

bool dfs(int r,int hd,int p)
{
    if(!r)
    {
        if(hd>=m-2) return true;
        int i;
        for(i=0;vis[i];i++);
        vis[i]=1;
        if(dfs(len-s[i],hd+1,0)) return true;
        vis[i]=0;
        return false;
    }
    else
    {
        for(int i=p;i<n;i++)
        {
            if(!vis[i] &&s[i]<=r)
            {
                vis[i]=1;
                if(dfs(r-s[i],hd,i+1)) return true;
                vis[i]=0;
                while(s[i+1]==s[i] && i<n) i++;
            }
        }
        return false;
    }
}

int main()
{
    while(~scanf("%d",&n) &&n)
    {
        l=sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&s[i]);
            sum+=s[i];
            l=max(l,s[i]);
        }
        sort(s,s+n,cmp);
        for(int i=l;l<=sum;i++)
        {
            if(sum%i) continue;
            m=sum/i,len=i;
            memset(vis,0,sizeof(vis));
            vis[0]=1;
            if(dfs(len-s[0],0,0))
            {
                printf("%d\n",len);
                break;
            }
            vis[0]=0;

        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值