hdu 1445 sticks (经典dfs+剪枝)
来源:互联网 发布:量化交易程序员 招聘 编辑:程序博客网 时间:2024/06/05 05:13
sticks
Problem DescriptionGeorge 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 file 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
#include<iostream>#include<string.h>#include<algorithm>using namespace std;int n,sum,len;int stick[64];int visit[64];bool cmp(int a,int b){return a>b;}int dfs(int num,int l){int i;if(num==0&&l==0) return 1;if(l==0) l=len;for(i=0;i<n;i++)if(!visit[i]&&stick[i]<=l){if(i>0&&!stick[i-1]&&stick[i]==stick[i-1]) continue;//与前一个相同的搜索visit[i]=1;if(dfs(num-1,l-stick[i])) return 1;else{visit[i]=0;if(stick[i]==l||l==len) return 0;/* BLACKBOOK:Page181 1,为了避免重复搜索,按照木棍的第一段长度递增的顺序搜索每根木棍, 在每根木棍中按照长度递减的顺序考虑每个可能的长度, 2, 如果某长度刚好能够填满一根原始木棍,就没有必要考虑其他木棍了; 如果使用更短的小木棍,即使存在某个也能填满该原始木棍的方法,该方法也一定不会 比用大木棍更有希望获得可行解 (比如a b c d e f,目标长度为a+b,且为失败搜索,则a+b之后即退出,比如b==d+e的话, 将b替换为d+e,若有成功搜索,则肯定有某个和为a,若有,它在a+b搜索之后就应该搜索过了和 [b+e]组合的,所以重复搜索) */ }}return 0;}int main(){int i;while(cin>>n&&n){sum=0;for(i=0;i<n;i++){cin>>stick[i];sum+=stick[i];}sort(stick,stick+n,cmp);//从大到小排序for(i=stick[0];i<=sum;i++)if(sum%i==0){len=i;memset(visit,0,sizeof(visit));if(dfs(n,len)) {cout<<len<<endl;break;}}}return 0;}
0 0
- hdu 1445 sticks (经典dfs+剪枝)
- hdu 1455 Sticks (dfs 经典剪枝)
- HDU-1455 Sticks 经典dfs剪枝
- Sticks (dfs经典剪枝)
- HDU 1455 Sticks(DFS+剪枝)
- hdu 1455 sticks dfs+剪枝
- hdu 1455 Sticks 【dfs剪枝】
- pku 1011 sticks 经典DFS+剪枝
- 307Sticks(*****)经典DFS+剪枝
- POJ 1011 Sticks 经典dfs + 剪枝
- poj 1011 Sticks 经典dfs+剪枝
- POJ 1011 Sticks 经典的dfs+剪枝
- HDU--1455 -- Sticks [DFS] [剪枝优化]
- hdu 1455 Sticks(dfs+可行性剪枝)
- hdu 1455 Sticks(DFS+剪枝)
- hdu 1455 Sticks(DFS+剪枝)
- HDU 1455 Sticks(dfs+强剪枝)
- POJ1011-Sticks DFS+剪枝
- HttpWatch工具简介及使用技巧(转载)
- HDU 2955 Robberies (01背包)
- hdu2177 取(2堆)石子游戏
- UVALive6663 Count the Regions
- 【VIM简明教程】
- hdu 1445 sticks (经典dfs+剪枝)
- java中try cathc finally return 相关异常问题
- [UVALive 6663 Count the Regions] (dfs + 离散化)
- 路由收敛
- 网络协议
- 分析USB平台设备模型框架(1)
- poj 1113 Wall 凸包模板题
- hdu 1083
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir