POJ1011 Sticks
2013-07-30 23:34
399 查看
题目来源:http://poj.org/problem?id=1011
题目大意:George有一些等长的木棍,他把这些木棍随机地砍断,砍断后的木棍最多50单位长。现在他想把木棍拼回原来的长度,但他已经忘记了原来有多少根木棍和原来的木棍有多长。请帮他设计一个程序计算原始木棍的最小长度。所有木棍的长度都是正整数单位长度。
输入:含多个数据块,每个数据块为一个测试用例,第一行为一个整数,表示砍断后有多少根木棍,最大为64.第二行为每根木棍的长度,以空格分开。输入的最后一行为0。
输出:每个测试用例对应一行,含一个整数:最小的可能原始木棍长度。
Sample Input
Sample Output
对问题进行分析发现,我们可以把可能解确定在一个范围内:
1.原始长度最小为砍断后最长的那根木棍的长度
2.因为木棍的总长度不变,那么原来木棍最长为现有所有短木棍拼起来的长度
3.因为木棍都为整数单位长度,且对木棍进行砍断后,木棍数目只会增加而不可能减少,所以若总长度不能整除一个候选解或某候选解对应的原始木棍根数多于现在木棍的根数,显然都不可能成为可行解
基于以上的思想,用DFS+剪枝实现。
View Code
题目大意:George有一些等长的木棍,他把这些木棍随机地砍断,砍断后的木棍最多50单位长。现在他想把木棍拼回原来的长度,但他已经忘记了原来有多少根木棍和原来的木棍有多长。请帮他设计一个程序计算原始木棍的最小长度。所有木棍的长度都是正整数单位长度。
输入:含多个数据块,每个数据块为一个测试用例,第一行为一个整数,表示砍断后有多少根木棍,最大为64.第二行为每根木棍的长度,以空格分开。输入的最后一行为0。
输出:每个测试用例对应一行,含一个整数:最小的可能原始木棍长度。
Sample Input
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
Sample Output
6 5
对问题进行分析发现,我们可以把可能解确定在一个范围内:
1.原始长度最小为砍断后最长的那根木棍的长度
2.因为木棍的总长度不变,那么原来木棍最长为现有所有短木棍拼起来的长度
3.因为木棍都为整数单位长度,且对木棍进行砍断后,木棍数目只会增加而不可能减少,所以若总长度不能整除一个候选解或某候选解对应的原始木棍根数多于现在木棍的根数,显然都不可能成为可行解
基于以上的思想,用DFS+剪枝实现。
////////////////////////////////////////////////////////////////////////// // POJ1011 Sticks // Memory: 284K Time: 16MS // Language: C++ Result: Accepted ////////////////////////////////////////////////////////////////////////// #include <iostream> #include <stdlib.h> using namespace std; bool buff[64]; int stick[70]; bool visit[70]; int n; int campare(const void * a, const void * b) { return *((int *)b) - *((int *)a); } bool dfs(int * stick, bool * visit, int len, int initLength, int start, int num) { if (num == n) { return true; } int sample = -1; for (int i = start; i < n; i++) { if (visit[i] == true || stick[i] == sample) { continue; } visit[i] = true; if (len + stick[i] == initLength) { if (dfs(stick, visit, 0, initLength, 0, num + 1)) { return true; } else { sample = stick[i]; } } else if (len + stick[i] < initLength) { if (dfs(stick, visit, len + stick[i], initLength, i, num + 1)) { return true; } else { sample = stick[i]; } } visit[i] = false; if (len == 0) break; } return false; } int main(void) { while (true) { int totalLength = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> stick[i]; visit[i] = false; totalLength += stick[i]; } qsort(stick, n, sizeof(int), campare); int maxLength = stick[0]; bool findSolution = false; //检验每个可能的可行解 for (int initLength = maxLength; initLength <= totalLength - initLength; initLength++) { //isSolution标记是否找到可行解 int oriCount = 0; //totalLength能被i整除,oriCount表示原始木棍数 if (totalLength % initLength != 0) { continue; } if(dfs(stick, visit, 0, initLength, 0, 0) == true) { cout << initLength << endl; findSolution = true; break; } } if (!findSolution) { cout << totalLength << endl; } } system("pause"); return 0; }
View Code
相关文章推荐
- poj 1011 Sticks(dfs+剪枝)
- poj1011 Sticks
- POJ 1011 Sticks
- POJ 1011 Sticks
- poj1011 Sticks
- POJ 百练1011 STICKS
- poj 1011-小白算法练习 Sticks 从最简单的做起 dfs+剪枝
- POJ1011 Sticks
- Poj_1011_Sticks(剪枝)
- TLE: poj 1011 Sticks
- poj 1011 Sticks
- poj 1011 Sticks (DFS+剪枝)
- Poj_1011_Sticks(剪枝)
- poj 1011 Sticks
- [POJ 1011] Sticks DFS神剪枝
- HDU 1455 POJ 1011 Sticks 搜索
- poj 1011 sticks
- poj 1011 Sticks(经典搜索问题:DFS+剪枝)
- POJ 1011 Sticks
- POJ 1011--Sticks