算法设计与分析作业3-1

3-1

古卡萨人为了建造一个高塔,先采集了大量不同类型的石块。每种类型的石块的高度为hi数量为ci。他们在修建高塔之前,先通过占卜确定每种类型的石块能够摆放的最大高度,然后再将他们一块一块垒起来,最终完成了高塔的建造。

请你根据现有石块的情况计算出他们最高能够建造出的高塔的高度。

输入要求:输入第1行为整数n,表示石块的类型。其后有n行,

每一行包含三个整数hi (1 <= hi <= 100) ,ai(1 <= ai <= 40000),ci(1 <= ci <= 10),

分别表示该类型的石块的高度,该类型石块能够摆放在塔上的最大高度以及该类型石块的数量。

样例输入:

3

7 40 3

5 23 8

2 52 6

样例输出:

48

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int maxheight = 0, n;
const int N = 1000;

typedef struct stone
{
    int hi, ai, ci;
} stone;

stone stones[N];

void dfs(int u, int curheight)
{
    if (u == n)
    { // 递归到叶子节点
        maxheight = max(curheight, maxheight);
        return;
    }
    // 迭代当前石头的可能数量
    for (int i = 0; i <= stones[u].ci; i++)
    {
        // 检查使用 'i' 个 'u' 类型的石头不会超过重量限制
        if (curheight + i * stones[u].hi <= stones[u].ai)
        {
            dfs(u + 1, curheight + i * stones[u].hi);
        }
    }
    // 调用 dfs,不使用当前石头
    dfs(u + 1, curheight);
}
bool cmp(stone a, stone b)
{
    return a.ai < b.ai;
}
int main()
{
    // cout << "请输入石头的种类:" << endl;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> stones[i].hi >> stones[i].ai >> stones[i].ci;
    sort(stones, stones + n, cmp);
    dfs(0, 0);
    cout << maxheight;
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值