【动态规划】Problem-1069 Monkey and Banana

本文详细解析了一道经典的砖块堆叠算法问题,通过动态规划方法寻找最高的砖块塔。文章提供了完整的代码实现,并附有详细的注释,帮助读者深入理解状态转移方程和排序策略。

动规题一直似懂非懂,今天做了道经典例题,加深理解。

下面给出原题

Problem Description

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. 

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked. 

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
 

 

Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
 
 
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".

 

 
Sample Input

 

1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27 0
 

 

Sample Output
 
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
 
 
题目大意就是堆砖块,给定n种无限多的砖块,问最高能堆多高。
砖块有长宽高,规定上层砖块的长要严格小于下层砖块的长,上层砖块的宽也要严格小于下层砖块的宽。
思路:
从底层往顶层考虑。假设最底层放的是第i个砖块,那么可堆的最高高度就等于它上层可堆的最高高度加上砖块i本身的高度。
以此类推,一直到顶层,也就是砖块长和宽都最小的那层(它的上层不可再放置其它砖块了),以它为最底层,可堆的最高高度即为它本身高度。
状态转移方程:dp[i] = max{dp[j]} + block[i].h
 
下面给出AC代码,注释写的较详尽:
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 struct brick{
 6     int l=0,w=0,h=0;  //长宽高
 7 }b[1000]; //砖块结构体数组
 8 int dp[1000];  //dp[i]: 以第i个砖块为最底层可得到的最高高度
 9 
10 bool cmp(brick b1,brick b2){  //先长后宽从小到大排序
11     if(b1.l==b2.l) return b1.w<b2.w;
12     return b1.l<b2.l;
13 }
14 
15 int main()
16 {
17     int n;
18     int x,y,z;
19     int kase = 0;
20     while(cin>>n && n){
21         int len = 0;
22         for(int i = 0; i < n; ++i){
23             cin>>x>>y>>z;
24             //每个砖块有三种放置方法(考虑长>宽)
25             b[len].h = x,b[len].l = y>z?y:z,b[len++].w = y>z?z:y;
26             b[len].h = y,b[len].l = x>z?x:z,b[len++].w = x>z?z:x;
27             b[len].h = z,b[len].l = x>y?x:y,b[len++].w = x>y?y:x;
28         }
29         sort(b,b+len,cmp);  //以先长后宽从小到大的顺序给砖块排序
30         dp[0] = b[0].h;  //现在的b[0]是最小的砖块,上面不能放置其他砖块,高度为它本身
31         int max_h;
32         for(int i=1;i<len;++i){   //从第二个砖块开始循环,每次记录最大值 把第i个箱子放在前i-1个箱子的某一个下面
33             max_h = 0;  //前面0到i-1个砖块可以堆放的最高高度
34             for(int j=0;j<i;++j){
35                 if(b[j].l<b[i].l && b[j].w<b[i].w)
36                     max_h = max_h>dp[j]?max_h:dp[j];
37             }
38             dp[i] = b[i].h + max_h;  //以第i个砖块为最底层可得到的最高高度=前i-1层可堆放的最高高度+i本身高度
39         }
40         sort(dp,dp+len);
41         cout<<"Case "<<(++kase)<<": maximum height = "<<dp[len-1]<<endl;
42     }
43 
44     return 0;
45 
46 }

参考题解:

https://blog.youkuaiyun.com/little_dro/article/details/81590810

https://www.cnblogs.com/superxuezhazha/p/5792691.html

 

转载于:https://www.cnblogs.com/Aikoin/p/10061206.html

# 代码概述 该请求基于“猴子摘香蕉”问题,要求将符号逻辑规划问题转化为可执行的 Python 程序。程序需模拟猴子通过行走、推箱、爬箱、摘香蕉等动作,从初始状态达到目标状态。 我们将使用类和谓词函数来建模环境状态与动作,确保每一步操作都满足先决条件,并更新全局状态。 --- # 代码解析 ```python class MonkeyBananaProblem: def __init__(self): # 初始状态 self.monkey_at = 'a' self.box_at = 'c' self.banana_at = 'b' self.on_box = False self.has_banana = False def walk(self, pos): """猴子从当前位置走到指定位置""" if self.on_box: print("猴子正在箱子上,无法行走。") return False print(f"猴子从 {self.monkey_at} 走到 {pos}") self.monkey_at = pos return True def push_box(self, to_pos): """猴子将箱子从当前位置推到新位置""" if not (self.monkey_at == self.box_at and not self.on_box): print("猴子必须和箱子在同一位置且不在箱子上才能推箱子。") return False print(f"猴子将箱子从 {self.box_at} 推到 {to_pos}") self.box_at = to_pos self.monkey_at = to_pos return True def climb_up(self): """猴子爬上箱子""" if self.monkey_at != self.box_at: print("猴子必须在箱子旁边才能爬上箱子。") return False if self.on_box: print("猴子已经在箱子上了。") return False print("猴子爬上了箱子。") self.on_box = True return True def grasp_banana(self): """猴子摘香蕉""" if self.on_box and self.box_at == self.banana_at and self.monkey_at == self.banana_at: print("猴子成功摘到了香蕉!") self.has_banana = True return True else: print("无法摘香蕉:需站在位于香蕉下方的箱子上。") return False def is_goal(self): """检查是否达到目标状态""" return self.has_banana and self.on_box and self.box_at == 'b' and self.monkey_at == 'b' def show_state(self): """打印当前状态""" print(f"当前位置 -> 猴子: {self.monkey_at}, 箱子: {self.box_at}, " f"香蕉: {self.banana_at}, 在箱子上: {self.on_box}, 已摘香蕉: {self.has_banana}") # 执行行动计划 if __name__ == "__main__": problem = MonkeyBananaProblem() print("初始状态:") problem.show_state() print("\n开始行动:") problem.walk('c') # 步骤1:从 a 走到 c problem.push_box('b') # 步骤2:将箱子从 c 推到 b problem.climb_up() # 步骤3:爬上箱子 problem.grasp_banana() # 步骤4:摘香蕉 print("\n最终状态:") problem.show_state() if problem.is_goal(): print("✅ 成功达成目标状态!") else: print("❌ 未达成目标状态。") ``` --- # 知识点 - **状态建模与封装**:使用类封装状态与行为,实现清晰的状态管理和方法调用,符合面向对象设计原则。 - **条件控制与动作验证**:每个动作前检查先决条件,防止非法状态转移,增强程序健壮性。 - **状态转移模拟**:通过顺序执行合法动作,模拟智能体在环境中的规划路径,体现经典AI问题求解思路。 写python代码,并且可以可视化
11-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值