POJ 1011 Sticks(DFS + 剪枝)

POJ 1011 Sticks

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 160960Accepted: 38639
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
题目大意

给出一定数量的长度的木棒,把他们拼成长度相同的木棒,并且要求拼接后长短尽量短,本题类似于POJ 2362还是一道使用DFS + 剪枝的思想

解题思路

整体还是使用DFS 搜索 +剪枝减去不必要的分支,在一定数量的木棒拼成长度相同的木棒,要求长短尽量短,假设拼接后每根木棒的长度为L,L长度应当是比拼接前每个木棒长度都长,依次枚举拼接后木棒长度,找到可以符合条件的就输出 大致看一下(需要剪枝的地方)过程:

  1. 首先求出所有的木棒长度之和SUM ,假设最短长度L 要求
    SUM % L == 0
  2. L的长度应当比此组木棒所有的长度都要长,即从最长的长度到SUM遍历找到最小的长度
  3. 搜素长度由大到小进行搜索 如果出现最长的就已将无法拼成L的话,那么之后的木棒当然在全局上就无法达成条件,就不用再继续拼接了,直接返回上一层。
  4. 由于已经进行了排序如果出现相同长度的木棒跳过 ,例如当前的木棒长度为 5 5 5 2 2 2 1 1 1 开始时选择的长度为5之后重复的搜索5就跳过 同理 长度为 2 搜索一次就跳过
解题:
#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int Max = 64;
/*木棒个数n ,假设当前木棒相同的长度len
	stick 保存木棒长度
	falg标记达成条件
	visit标记已经访问过
*/
int n, len, stick[Max];
bool flag, visit[Max];

bool cmp(int a, int b){
    return a > b;
}
//num 深度即当前使用的木棒数 curlen 当前拼接长度 s起始位置
void dfs(int num, int curlen, int s){
   // 递归结束
    if(flag) return;
    if(curlen == 0){
        int k = 0;
       //找到最大长度
        while(visit[k]) k++;
        visit[k] = true;
        dfs(num + 1, stick[k], k + 1);
        visit[k] = false;
        return;
    }
   	//当前出现一个木棒拼接成len
    if(curlen == len){
       //如果全部木棒都已经使用
        if(n == num) flag = true;
       //没全部使用继续搜索
        else dfs(num, 0, 0);
        return ;
    }
    for(int i = s; i < n; i++){
        if(!visit[i] && curlen + stick[i] <= len){
           //出现长度相同的情况过滤 剪枝
            if(!visit[i - 1] && stick[i] == stick[i - 1]) continue;
            visit[i] = true;
            dfs(num + 1, curlen + stick[i], i + 1);
            visit[i] = false;
        }
    }
}
int main(){
    while(scanf("%d", &n) && n != 0){
        int sum = 0;
        flag = false;
        for(int i = 0; i < n; i++) {
            cin >> stick[i];
            sum += stick[i];
        }
       //将数值有小到大排序 剪枝
        sort(stick, stick + n, cmp);
        for(len = stick[0]; len < sum; len++){
         //不符合整除条件 剪枝
            if(sum % len == 0) {
                dfs(0, 0, 0);
                if(flag) break;
            }
        }
        printf("%d\n", len);
    }
}
总结

本篇主要用于自己学习理解 没理解的地方可以参考这里,主要都是在人家那里参考学习的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值