poj1011 _经典搜索
来源:互联网 发布:微信支付退款接口 php 编辑:程序博客网 时间:2024/06/06 05:48
Sticks
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 133220 Accepted: 31285
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
/**************************************
经典搜索题,剪枝众多。。。上代码
**************************************/
</pre><div><pre name="code" class="cpp">#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <bitset>using namespace std;#define INT_MAX 1 << 30typedef long long ll;int n;int sum;int a[100];int used [100];int l;int cmp(const void *a,const void *b) { return *(int *)b - *(int *)a;}int dfs(int num,int length) //剩下的木棍数和拼接需要的棍子长度{ if(num == 0 && length == 0) return 1; //搜索结束 if(length == 0) length = l; //搜索的继续 for (int i = 0; i < n; i += 1) { if(!used[i]&&(a[i] <= length)) //木棒的长度必须小于需要的长度 { if (i > 0) //排序后相等的相邻,如果i失败了相等的i-1跳过,剪枝4 { if(!used[i-1]&&(a[i] == a[i-1])) { continue; } } used[i] = 1; if(dfs(num-1,length-a[i])) return 1; else { used[i] = 0; //搜索失败回溯,深搜的鲜明特征 if(length == a[i]) return 0; //棍子的最后一根短棒失败,不必找更短棒代替 if(length == l) return 0; //棍子的第一根长棒失败,后面短棒不必再循环 } } } return 0;}int main(){ while ((scanf("%d",&n) != EOF) && (n != 0)) { sum = 0; memset(a,0,sizeof(a)); //初始化 for (int i = 0; i < n; i += 1) { cin >> a[i]; sum += a[i]; } qsort(a,n,sizeof(a[0]),cmp); //从大到小进行排序,提高搜索效率,剪枝1 for (l = a[0]; l <= sum/2; l += 1)//棍子的最短长度不能低于木棒的最长长度,剪枝2 { if (sum%l) //非因子直接跳过,剪枝3 { continue; } memset(used,0,sizeof(used)); //每次搜索都要初始化 if (dfs(n,l)) //由于搜索最短木棒,成功后直接跳出 { cout << l << endl; break; } } if(l > sum/2) cout << sum << endl; } return 0;}欢迎指错与讨论
1 0
- poj1011 _经典搜索
- POJ1011 Sticks解题报告(经典搜索)
- poj1011 搜索
- poj1011 搜索
- [搜索]poj1011
- poj1011(搜索应用)
- poj1011 Sticks 搜索
- poj1011 Sticks(搜索+剪枝)
- poj1011 Sticks(搜索剪枝)
- HDU1455 POJ1011 Sticks 搜索
- 【DFS搜索】poj1011 Sticks
- POJ1011 搜索+剪枝【很好】
- 搜索算法(poj1011)
- POJ1011(经典dfs)
- POJ1011 经典DFS+剪枝
- 第九天:poj1011(又是深度搜索+剪枝,还是不会o(∩_∩)o )
- hdu1455 && poj1011 Sticks(深度优先搜索 DFS 经典剪枝 详解)
- poj1011 stick 强力搜索剪枝
- json简单实例
- HDOJ 1159....DP入门
- servlet 详解1
- block 用copy 原因
- php的时间转换
- poj1011 _经典搜索
- 占座2016
- cocos2d-html5 各平台声音播放总结
- oracle 查看正在执行的sql_trace
- MytabIcon(一个自定义的控件,且带有渐变效果)
- POJ-2594-Treasure Exploration-(路径相交的最小路径覆盖+闭包传递)
- 用requestDisallowInterceptTouchEvent()方法防止viewpager和子view滑动事件冲突
- Logistic回归(改进的随机梯度上升)
- struct结构体在c和c++中的区别