ACM: 一题DFS(深搜,剪枝) poj 1011
2016-05-19 23:17
405 查看
Sticks
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
题意: 给你n根小木棒,找出能恰好拼凑成最大的长度.
例如第一个例子: (5,1) , (5,1) , (5,1) , (2,2,2) => 6
第二个例子: (2,3) , (1,4) => 5
解题思路:
1. 一开始想到是动态规划,像背包问题.
2. 用深搜的思想试一试,木棒长度的尝试法. len = stick[0] ; len
<= sum; len++;
3. dfs(深搜就要涉及剪枝问题).
递归的参数: 尝试长度len , 离目标还需要长度reLen , 木棒剩余条数: num;
问题分析:
(1).递归入口: 因为木棒拼凑成的条数是整数,所以一开始可以剪枝一次,sum % len == 0
(2).确定递归的出口是: 当剩余木棒条数为0时, 并且reLen = 0;
循环递归中:
(3).当木棒被访问过continue;
(4).reLen 必须要大于等于 当前木棒的长度(reLen >= stick[i]);
(5).在(4)的基础上可以再次剪枝 stick[i] == reLen || reLen == len
(
解释: 本来表示即将拼成一根棒,或刚进行新一轮拼凑
(即if中判断的是主问题性质完全相同的子问题)
但经过上面的if判断,子问题不能完成所有任务,
那么整体不可能完成任务,不再考虑,搜索失败(剪枝)
)
(6).将相同长度的木棒跳跃(剪枝);(相同不能解决问题的子问题进行剪枝).
代码:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAX 70
int stick[MAX];
bool visited[MAX];
int n;
int sum;
int cmp(const void *a,const void *b)
{
return
*(int*)b - *(int*)a;
}
int dfs(int len,int reLen,int num)
{
if(reLen ==
0 && num == 0)
return len;
if(reLen ==
0)
reLen = len;
for(int i =
0; i < n; ++i)
{
if(visited[i] == 1)
continue;
if(reLen >= stick[i])
{
visited[i] = 1;
if(dfs(len,reLen-stick[i],num-1) != 0)
return len;
visited[i] = 0;
if(stick[i] == reLen || reLen == len)
break;
while(stick[i] == stick[i+1])
i++;
}
}
return
0;
}
int main()
{
//
freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF
&& n != 0)
{
sum = 0;
for(int i = 0; i < n; ++i)
{
scanf("%d",&stick[i]);
sum += stick[i];
}
qsort(stick,n,sizeof(stick[0]),cmp);
int result;
for(int len = stick[0]; len <= sum; ++len)
{
if(sum % len == 0)
{
result = 0;
memset(visited,0,sizeof(visited));
result = dfs(len,0,n);
if(result != 0)
break;
}
}
printf("%d\n",result);
}
return
0;
}
相关文章推荐
- ACM: 一题STL map题 poj 1002 (bt…
- 20160519 学习日志
- Android ListFragment 中给LinearLayout添加点击事件
- hdu_4714_Tree2cycle(树形DP)
- IIS中报错弹出调试,系统日志-错误应用程序名称: w3wp.exe,版本: 8.5.9600.16384,时间戳: 0x5215df96(360主机卫士)
- ACM: 图论题 poj 1062 (邻接矩阵+d…
- ACM: 图论题 poj 2253 (poj上的dou…
- ACM: 图论题 poj 1125 (题意读好久…
- ACM: 图论题 poj 2240 (floyd + ST…
- ACM: 图论题 poj 1789 (一次AC的水…
- ACM: 图论题 poj2485 (crazy now~!…
- ACM: 图论题 BFS + STL + priority…
- ACM: 方向dfs + bfs求最短路 poj 3…
- ACM: 一题组合数学题 poj1850 (头…
- ACM: 一题数论题(扩展欧几里得定理…
- ACM: 一题组合数学题 poj1942 (细…
- ACM: 一题二分搜索题 poj3273 (…
- 网络多线程02
- ACM: 一题二分搜索题. poj3258 & 4000 nbsp;…
- 【LintCode】查找斐波那契数列中的N个数