POJ 1416 碎纸机

博客介绍了如何解决POJ 1416问题,即对一个数字进行分割,使得分割后的数字之和不超过目标值并尽可能接近目标值。通过深度优先搜索算法进行求解,并实施了剪枝策略以优化搜索过程。整个搜索空间可视作一个满二叉树,在不剪枝的情况下。博客提供了具体的搜索路径示例,有助于理解和应用类似问题的解决方案。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

static int target, n;
static int result;
static int digit[8];
static int instant[8];
static int solution[8];
static int p;
static int splits;
static int mmax;

//#define DEBUG

static void search_dfs(int exist, int cur, int sum)
{
	if (sum > target)
		return;

	if (cur >= n)
	{
		if (sum == mmax)
		{
			result++;
		//	target = -1;
		}
		else if (sum > mmax)
		{
			mmax = sum;
			int i;
			splits = p; result = 1; 
			for (i = 0; i < p; i++)
			{
				solution[i] = instant[i];
			}
		}
	}
	else
	{
		int tmp = exist * 10 + digit[cur]; // if tmp > target ?
        instant[p++] = tmp;
		search_dfs(0, cur + 1, sum + tmp);
		instant[p--] = 0;
		if (cur < n - 1)
			search_dfs(tmp, cur + 1, sum);
	}
}

int main()
{
#ifdef DEBUG
	fstream cin("G:\\book\\algorithms\
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值