POJ1011 Sticks 【剪枝】

Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 122771   Accepted: 28441

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

Source

Central Europe 1995

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

int Len[66], tarLen, tarCnt, N, sum, cnt;
bool vis[66];

bool DFS(int k, int leftLen) {
	int i;
	if(leftLen == 0) {
		if(++cnt == tarCnt) return true;
		for(i = 0; i < N; ++i)
			if(!vis[i]) {
				vis[i] = 1;
				if(DFS(i + 1, tarLen - Len[i]))
					return true;
				else {
					vis[i] = 0;
					--cnt;
					return false;
				}
			}
	}
	for(i = k; i < N; ++i) {
		if(!vis[i] && Len[i] <= leftLen) {
			if(Len[i] == Len[i-1] && !vis[i-1])
				continue;
			vis[i] = 1;
			if(DFS(i + 1, leftLen - Len[i]))
				return true;
			vis[i] = 0;
		}
	}
	return false;
}

int main() {
	int i, j;
	while(scanf("%d", &N), N) {
		for(i = sum = 0; i < N; ++i) {
			scanf("%d", &Len[i]);
			sum += Len[i];
		}
		sort(Len, Len + N, greater<int>());
		for(tarLen = Len[0]; tarLen < sum; ++tarLen) {
			if(sum % tarLen) continue;
			tarCnt = sum / tarLen;
			memset(vis, 0, sizeof(vis));
			vis[0] = 1; cnt = 0;
			if(DFS(1, tarLen - Len[0])) break;
		}
		printf("%d\n", tarLen);
	}
	return 0;
}
时间: 2024-11-10 00:16:38

POJ1011 Sticks 【剪枝】的相关文章

poj1011 Sticks DFS+回溯

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=1011 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

POJ1011 Sticks DFS+剪枝

Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包含多组数据,每组数据包括两行.第一行是一个不超过64的整数,表示砍断之后共有多少节木棍.第二行是截断以后,所得到的各节木棍的长度.在最后一组数据之后,是一个零. Output 为每组数据,分别输出

poj1011 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 original

poj-1011 sticks(搜索题)

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 h

poj1011 搜索+剪枝

DFS+剪枝 POJ2362的强化版,重点在于剪枝 令InitLen为所求的最短原始棒长,maxlen为给定的棒子堆中最长的棒子,sumlen为这堆棒子的长度之和,那么InitLen必定在范围[maxlen,sumlen]中 根据棒子的灵活度(棒子越长,灵活度越低) DFS前先对所有棒子降序排序 剪枝: 1. 由于所有原始棒子等长,那么必有sumlen%Initlen==0: 2. 若能在[maxlen,sumlen-InitLen]找到最短的InitLen,该InitLen必也是[maxlen

POJ2676 Sticks

木棒 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 147267   Accepted: 34890 Description 乔 治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多 少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包含多组数据,每

poj练习题的方法

poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1077--Eightpoj1084--Square Destroyerpoj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝)poj1088--滑雪poj1129--Channel Allocation 着色问题 dfspoj1154--letters (dfs)p

POJ 搜索题集

poj1010--邮票问题 DFS poj1011--Sticks dfs + 剪枝 poj1020--拼蛋糕 poj1054--The Troublesome Frog poj1062--昂贵的聘礼 poj1077--Eight poj1084--Square Destroyer poj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝) poj1088--滑雪 poj1129--Channel Allocation 着色问题 dfs poj1154--lett

50道hdu基础搜索总结(转)

Dfs: 大部分是直接递归枚举,即求满足约束条件下的解,虽不用剪枝,但也需要代码能力. 练习递归枚举的题目: 1241       Oil Deposits (dfs的连通块个数) 1016       Prime Ring Problem 1584       蜘蛛牌(简单dfs,简单的剪枝,还有人用DP做(???)) 1426       Sudoku Killer(练习递归的好题目 or Dancing links(???)) 2510       符号三角形(打表题,写写打表程序还是不错