poj1011 -- Sticks (DFS+剪枝)

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 143809 Accepted: 33965

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个更短的棍子,输入数据n,接下来输入n个短棍子的长度,输出被剪之前的棍子可能的最短长度len。从题目中可以得出:1、最后得到的结果一定可以被n个短棍子长度的和整除,因为最初的棍子的长度都是相同的;2、棍子最初的长度一定大于等于最长的短棍子的长度,可以利用这一点来减少枚举量(最初棍子的长度不用从1开始枚举,而是从最长的短棍子长度开始枚举)。

首先本题是使用dfs来求解,但输入的短棍子数目n的最大值为64,所以如果单纯的使用dfs而不剪枝的话肯定会超时。剪枝即去掉一些无用的状态不去搜索,从而加快搜索速度。本题的剪枝可以从以下几个角度考虑:

1、将输入的短棍子长度从大到小排序;

2、如果在组合短棍子的过程中,短棍子sticks[i-1]没有被使用,则如果下一个短棍子sticks[i]的长度与stick[i-1]的长度相同,则可直接跳过;

3、每次组合棍子的时候,总是从最长的未被使用的短棍子开始组合,因为如果最长的棍子搜索不成功,那么比它短的棍子可能会成功,但最后也不会成功,所以我们可以认为如果最长的棍子搜索不成功,那么此次搜索就是失败的。比如短棍子长度为5,4,4,3,2,如果现在尝试的最初棍子长度为6,则从5开始搜索得不到6,则即使剩下的4,4,3,2可以拼成6,但5最终没有被使用,所以搜索完5之后就没有搜索下去的必要了,我认为这一点是最重要的一点。

具体代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int visit[100];		//棍子是否访问过,0未访问,1已访问
int sticks[100];	//棍子的长度
int sum = 0;	//棍子的长度之和
int n;		//棍子的个数

bool cmp(int a, int b)	//将棍子从长到短排序的比较函数
{
	return a>b;
}

/*
 * stickLen:当前尝试的最初棍子的长度
 * nums:已成功组合完成的棍子的个数
 * leftLen:当前正在组合的棍子的剩余长度
 * index:遍历sticks[]数组的下标
*/

int dfs(int stickLen, int nums, int leftLen, int index)
{
	if (nums == sum / stickLen && leftLen == 0)
		return stickLen;
	if (leftLen == 0)
		leftLen = stickLen;

	for (int i = index;i<n;i++)
	{
		if (visit[i] || leftLen<sticks[i] || (i && !visit[i-1] && sticks[i] == sticks[i-1]))	//剪枝2
			continue;
		if (leftLen - sticks[i] == 0)
		{
			visit[i] = 1;
			if (dfs(stickLen, ++nums, 0, 0))
				return stickLen;
			visit[i] = 0;
			return 0;	//剪枝3
		}
		else if (leftLen - sticks[i] > 0) {
			visit[i] = 1;
			if (dfs(stickLen, nums, leftLen - sticks[i], i + 1))
				return leftLen;
			visit[i] = 0;
			if (leftLen == stickLen) return 0;	//剪枝3
		}
	}
	return 0;
}

int main()
{
	//freopen("test.txt", "r", stdin);
	while (scanf("%d", &n) == 1 && n)
	{
		sum = 0;
		memset(sticks, 0, sizeof(sticks));
		for (int i = 0;i<n;i++)
		{
			scanf("%d", &sticks[i]);
			sum += sticks[i];
		}
		sort(sticks, sticks + n, cmp);	//将棍子从长到短排序
		for (int stickLen = sticks[0];stickLen <= sum;stickLen++)
		{
			if (sum%stickLen != 0)
				continue;
			memset(visit, 0, sizeof(visit));
			int leftLen = stickLen;
			int ans = dfs(stickLen, 0, leftLen, 0);
			if (ans)
			{
				cout << ans << endl;
				break;
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值