POJ 1011 Sticks 【DFS 剪枝】

本文详细介绍了如何解决POJ 1011 Sticks问题,通过降序排序小木棍长度并枚举可能的原始木棍长度来寻找最小可能的原始木棍长度。

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

题目链接:http://poj.org/problem?id=1011

Sticks

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 154895 Accepted: 37034

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

 

题意概括:

George这家伙一开始把几条长度一样的木棍切成了好多小木棍,后来忘记原来有几条长度一样的木棍了,所以请我们帮帮忙把这些小木棍拼成尽可能长的长度相同的长木棍,输出长度。

解题思路:

先根据小木棍的长度降序排序

枚举木棍长度(可以整除小木棍总长度),DFS凑这些木棍。

剪枝:如果当前这条小木棍不能凑则这一类小木棍都凑不了(这也是要排序的原因)

 

AC code:

 1 ///poj 1011 dfs
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <cmath>
 7 #define INF 0x3f3f3f3f
 8 using namespace std;
 9 
10 const int MAXN = 70;
11 bool vis[MAXN];
12 int sticks[MAXN];
13 int N, sum, len;
14 bool flag;
15 
16 bool cmp(int x, int y)
17 {
18     return x>y;
19 }
20 
21 void dfs(int dep, int now_len, int st)  ///当前已经使用的木条数目,当前木条的长度, 需要处理的木条的下标
22 {
23     if(flag) return;
24     if(now_len == 0)       ///找木棍头
25     {
26         int k = 0;
27         while(vis[k]) k++;
28         vis[k] = true;
29         dfs(dep+1, sticks[k], k+1);
30         vis[k] = false;
31         return;
32     }
33     if(now_len == len)
34     {
35         if(dep == N) flag = true;   ///找到满足条件的len了
36         else dfs(dep, 0, 0);   ///没有用完,开始凑新的一根长度为len的木棍
37         return;
38     }
39     for(int i = st; i < N; i++)
40     {
41         if(!vis[i] && now_len + sticks[i] <= len)
42         {
43             if(!vis[i-1] && (sticks[i-1] == sticks[i])) continue;
44             vis[i] = true;
45             dfs(dep+1, now_len+sticks[i], i+1);
46             if(flag) return;
47             vis[i] = false;
48         }
49     }
50 }
51 
52 void init()
53 {
54     memset(vis, 0, sizeof(vis));
55     flag = false;
56     sum = 0;
57 }
58 
59 int main()
60 {
61     while(~scanf("%d", &N) && N )
62     {
63         init();
64         for(int i = 0; i < N; i++)
65         {
66             scanf("%d", &sticks[i]);
67             sum+=sticks[i];
68         }
69         sort(sticks, sticks+N, cmp);
70         for(len = sticks[0]; len < sum; len++)
71         {
72             if(sum%len==0)
73             {
74                 dfs(0, 0, 0);
75                 if(flag) break;
76             }
77         }
78         printf("%d\n", len);
79     }
80     return 0;
81 }

 

转载于:https://www.cnblogs.com/ymzjj/p/9497068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值