POJ 1011 Sticks(DFS+剪枝)详细注释

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

Sticks
Time Limit: 1000MS

Memory Limit: 10000K
Total Submissions: 147064

Accepted: 34847
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

题意:乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍 的长度都不超过50个长度单位。然后他又想把这些木棍恢复到为 裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。 请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节 木棍的长度都用大于零的整数表示。

思路:先统计出所有小棒的总长度,然后只枚举能被总长度整除的长度即可。对于枚举的每一个长度,按照长度从大到小的顺序DFS搜索。一步很重要的剪枝,如果一根小棒的长度不能拼成原棒,那么和他相等长度的小棒都不能拼成原棒,可以避免重复搜索。

附上ac代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100];
bool vis[100];           //用来标记每一根小棒是否被用过
int n, ans;
bool flag = false;       //用来标记是否拼完所有小棒
int cmp(int a, int b){
     return a > b;
}
void dfs(int x, int len, int u){   //x为当前已被用过的小棒数,len为当前长度,u为当前要处理的小棒
     if(flag)            //如果已经在之前拼完了所有小棒,退出搜索
          return;
     if(len == 0){       //当前长度为0,寻找下一个当前最长小棒
          int k = 0;
          while(vis[k])  //找到下一根没有用过的小棒
               k++;
          vis[k] = true;
          dfs(x + 1, a[k], k + 1);
          vis[k] = false;
          return;
     }
     if(len == ans){     //当前长度为ans, 即又拼成了一根原棒
          if(x == n)     //完成的标志,所有的n根小棒都拼成了
               flag = true;
          else           //拼下一根小棒
               dfs(x, 0, 0);
          return;
     }
     for(int i = u; i < n; i++){              //当前小棒完成了一部分
          if(!vis[i] && len + a[i] <= ans){
               if(!vis[i-1] && a[i] == a[i-1])//不重复搜索,最重要的剪枝
                    continue;
               vis[i] = true;
               dfs(x + 1, len + a[i], i + 1);
               vis[i] = false;
          }
     }
}
int main(){
     while(cin >> n && n){
          int s = 0;
          flag = false;
          for(int i = 0; i < n; i++){
               cin >> a[i];
               s += a[i];
          }
          sort(a, a + n, cmp);     //将小棒从大到小排序,也算是剪枝吧
          for(ans = a[0]; ans < s; ans++){
               if(s % ans == 0){   //枚举能被s整除的长度
                    memset(vis, 0, sizeof(vis));
                    dfs(0, 0, 0);
                    if(flag)
                         break;
               }
          }
          cout<<ans<<endl;
     }
     return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值