HDU 1455 POJ 1011 Sticks 搜索
2015-04-25 21:11
411 查看
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20608
**
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 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
这个题搜索加减枝,这个去年做过的题目,现在做起来还是手残了,敲挺久的,就在分支的计算上失误了。。。手残党。。。。不该!
** 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 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<bits/stdc++.h> using namespace std; const int N = 70; #define fi(i,n) for(int i = 0; i < n; i ++) #define fin(i,n1,n2) for(int i = n1; i < n2; i ++) int n, stick_sum, sum, sticks ; bool vis ; bool cmp(int a, int b) {return a > b;} bool dfs(int cnt, int st, int len) { if(cnt == n) return 1; int sample = -1; fin(i,st,n) if(!vis[i] && sticks[i] != sample) { vis[i] = 1; if(sticks[i] + len < stick_sum) { if(dfs(cnt + 1, i + 1, len + sticks[i])) return 1; else sample = sticks[i]; } else if(sticks[i] + len == stick_sum) { if(dfs(cnt + 1, 0, 0)) return 1; else sample = sticks[i]; } vis[i] = 0; if(!len) break; } return 0; } int main() { while(~scanf("%d", &n), n) { sum = 0; memset(vis, 0, sizeof(vis)); fi(i,n) { scanf("%d", sticks + i); sum += sticks[i]; } sort(sticks, sticks + n, cmp); for(stick_sum = sticks[0]; stick_sum <= sum; stick_sum ++) if(sum % stick_sum == 0 && dfs(0, 0, 0)) { printf("%d\n", stick_sum); break; } } return 0; }
相关文章推荐
- hdu 1455 poj 1011 sticks
- HDU_1455 && POJ_1011 Sticks (dfs)
- hdu 1455/poj 1011 Sticks(DFS剪枝神题)
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
- HDU 1455 POJ 1011 UVA 307 Sticks
- poj 1011--Sticks(搜索)
- poj 1011 Sticks 深度搜索+(剪枝)
- POJ 1011 STICKS 搜索 剪枝
- poj 1011 hdoj 1455 Sticks(搜索+剪枝)
- poj 1011 Sticks 搜索
- poj 1011 hdoj 1455 Sticks(搜索+剪枝)
- POJ 1011-Sticks 简单搜索DFS
- 【DFS搜索】poj1011 Sticks
- POJ 1011:Sticks 经典搜索
- POJ-1011-Sticks(经典搜索)
- poj 1011 Sticks(经典搜索问题:DFS+剪枝)
- POJ 1011:Sticks 经典搜索
- 【DFS迭代加深搜索+剪枝】POJ_1011_Sticks
- 杭电hdu 1455 sticks 深度优先搜索
- POJ 1011/HDU 1455 dfs 剪枝