码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1011 Sticks 【DFS 剪枝】

时间:2018-08-18 13:51:46      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:pre   需要   source   原来   integer   长度   long   central   string   

题目链接: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 }

 

POJ 1011 Sticks 【DFS 剪枝】

标签:pre   需要   source   原来   integer   长度   long   central   string   

原文地址:https://www.cnblogs.com/ymzjj/p/9497068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!