POJ 1011 Sticks dfs+剪枝

本文介绍了一个关于计算由若干部分组成原始木棒最短可能长度的问题。通过递归深度优先搜索的方法,寻找能够重新组合成原始木棒的所有可能长度,并确定最小的那个。输入包括切割后的木棒部分数量及各部分长度,输出则是原始木棒的最小可能长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 129130 Accepted: 30264

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

Source

AC代码:

<pre name="code" class="cpp">#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
///l->原来木棒的长度 num->原来木棒的数量 sum->木棒长度总和 n->现在木棒的数量
int sticks[65],l,num,sum,n;
///判断是否木棒被用
bool ismark[65];
bool cmp(int a,int b){
    return a>b;
}
///Snumber->已经组成的木棒数量 len->当前组合木棒的长度 pos->当前用到的木棒的位置(递减排序)
bool dfs(int Snumber,int len,int pos){
    int i;
    bool flag;
    if(len)flag=false;///用于下面判断
    else flag=true;
    if(Snumber==num)return true;///如果当前组成木棒的数等于原来的木棒的数 返回
    for(i=pos;i<n;i++){///否则继续组合木棒
        if(ismark[i])continue;///如果木棒用过 跳过
        if(len+sticks[i]==l){///如果当前木棒恰好能组合
            ismark[i]=true;///当前木棒标记用过
            if(dfs(Snumber+1,0,0))///进行下一条木棒的组合
                return true;
            ismark[i]=false;///失败 还原木棒
            return false;
        }
        else if(len+sticks[i]<l){///如果加上当前木棒不够构成所需要的木棒
            ismark[i]=true;///当前木棒标记用过
            if(dfs(Snumber,len+sticks[i],i))///继续搜下一个木棒来完成当前木棒的组合
                return true;
            ismark[i]=false;
            if(flag)return false;
            /**如果发现当前拼出的木棒长度为0结束本次搜索
            构建新棒时,对于新棒的第一根棒子,在搜索完所有棒子后都无法组合
            则说明该棒子无法在当前组合方式下组合,
            不用往下搜索(往下搜索会令该棒子被舍弃),直接返回上一层
            **/
            while(sticks[i]==sticks[i+1])i++; ///相同长度的木棍不要搜索多次
            ///这样长度的木棒不能构成所需要的木棒长度就不要在搜同样长度的木棒
        }
    }
    return false;
}
int main(){
    while(cin>>n&&n){
        sum=0;
        for(int i=0;i<n;i++){
            cin>>sticks[i];
            sum+=sticks[i];
        }
        sort(sticks,sticks+n,cmp);///递减排序 拼接木棒先用大的在用小的
        ///cout<<"sum "<<sum<<" sticks[0] "<<sticks[0]<<'\12';
        for(l=sticks[0];l<=sum;l++){///从最大的木棒开始搜
            if(sum%l==0){///如果满足条件
                num=sum/l;///求出原来的木棒数量
                memset(ismark,false,sizeof(ismark));///每次都要重置一下
                if(dfs(0,0,0)){///如果成功输出并退出
                    cout<<l<<'\12';
                    break;
                }
            }
        }
    }
    return 0;
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值