POJ 1011 Sticks dfs+剪枝
来源:互联网 发布:2016网络经典小说 编辑:程序博客网 时间:2024/05/29 15:58
Sticks
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 129130 Accepted: 30264
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
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
Source
Central Europe 1995
AC代码:
<pre name="code" class="cpp">#include <iostream>#include <cstring>#include <algorithm>using namespace std;///l->原来木棒的长度 num->原来木棒的数量 sum->木棒长度总和 n->现在木棒的数量int sticks[65],l,num,sum,n;///判断是否木棒被用bool ismark[65];bool cmp(int a,int b){ return a>b;}///Snumber->已经组成的木棒数量 len->当前组合木棒的长度 pos->当前用到的木棒的位置(递减排序)bool dfs(int Snumber,int len,int pos){ int i; bool flag; if(len)flag=false;///用于下面判断 else flag=true; if(Snumber==num)return true;///如果当前组成木棒的数等于原来的木棒的数 返回 for(i=pos;i<n;i++){///否则继续组合木棒 if(ismark[i])continue;///如果木棒用过 跳过 if(len+sticks[i]==l){///如果当前木棒恰好能组合 ismark[i]=true;///当前木棒标记用过 if(dfs(Snumber+1,0,0))///进行下一条木棒的组合 return true; ismark[i]=false;///失败 还原木棒 return false; } else if(len+sticks[i]<l){///如果加上当前木棒不够构成所需要的木棒 ismark[i]=true;///当前木棒标记用过 if(dfs(Snumber,len+sticks[i],i))///继续搜下一个木棒来完成当前木棒的组合 return true; ismark[i]=false; if(flag)return false; /**如果发现当前拼出的木棒长度为0结束本次搜索 构建新棒时,对于新棒的第一根棒子,在搜索完所有棒子后都无法组合 则说明该棒子无法在当前组合方式下组合, 不用往下搜索(往下搜索会令该棒子被舍弃),直接返回上一层 **/ while(sticks[i]==sticks[i+1])i++; ///相同长度的木棍不要搜索多次 ///这样长度的木棒不能构成所需要的木棒长度就不要在搜同样长度的木棒 } } return false;}int main(){ while(cin>>n&&n){ sum=0; for(int i=0;i<n;i++){ cin>>sticks[i]; sum+=sticks[i]; } sort(sticks,sticks+n,cmp);///递减排序 拼接木棒先用大的在用小的 ///cout<<"sum "<<sum<<" sticks[0] "<<sticks[0]<<'\12'; for(l=sticks[0];l<=sum;l++){///从最大的木棒开始搜 if(sum%l==0){///如果满足条件 num=sum/l;///求出原来的木棒数量 memset(ismark,false,sizeof(ismark));///每次都要重置一下 if(dfs(0,0,0)){///如果成功输出并退出 cout<<l<<'\12'; break; } } } } return 0;}
0 0
- poj 1011Sticks(DFS +剪枝)
- POJ 1011 Sticks dfs + 剪枝
- POJ 1011 Sticks DFS+剪枝
- poj 1011 Sticks -----dfs+剪枝
- POJ 1011 Sticks (DFS + 剪枝)
- POJ 1011 Sticks DFS 剪枝
- poj 1011 Sticks 【DFS】+【剪枝】
- [POJ 1011]Sticks(DFS剪枝)
- poj 1011 :Sticks (dfs+剪枝)
- POJ 1011 Sticks dfs+剪枝
- poj 1011 Sticks(dfs+剪枝)
- POJ 1011 Sticks DFS+剪枝
- POJ 1011 sticks DFS+剪枝
- poj 1011 Sticks (DFS剪枝)
- poj-1011-Sticks-(dfs+剪枝)
- poj 1011 Sticks DFS 剪枝
- poj 1011 Sticks解题报告【DFS+剪枝】
- poj 1011 sticks (dfs+剪枝)
- windows平台下的换行
- #ifndef/#define/#endif使用详解
- Binary Tree Level Order Traversal I & II
- oracle中sql语句的优化(转帖)
- jdk1.5新特性之自动装箱与拆箱
- POJ 1011 Sticks dfs+剪枝
- Interleaving String
- Linux 简介
- 定位图像中正方形区域
- POJ-2864 Pascal Library-年年参加聚会
- python机器学习库 :matplotlib
- [淘宝经验] - HBASE在淘宝网的应用和优化小结
- C++对象模型——指向Data Members的指针(Pointer to Data Members)(第三章)
- 埃拉托色尼筛选法: