8.动态规划-数字塔

问题描述

 Please implement solution for Number Tower with any programming language and analyze its time complexity.

翻译

数塔问题是一个经典的动态规划问题,它描述了一个由数字组成的三角形结构,要求从顶部开始向下走,每次只能走到相邻的位置,最终到达底部,使得经过的数字之和最大。数塔问题可以用一个二维数组来表示,其中第i行有i个元素,表示第i层的数字。例如:

9
12 15
10 6 8
2 18 9 5
16 12 18 10 8

数塔问题的一个可能的最优路径是:9 -> 15 -> 8 -> 9 -> 18,其和为59。

思路

首先将dp数组初始化为0,将最下方一排初始化为 数塔最低一排的数字

然后自底向上递归,每次和左右两个数相加,取最大值,一直到顶部为止

dp[i][j]=max(dp[i][j],dp[i][j+1])+a[i][j]

(此文代码是为了回溯才没用上述状态方程,都但是思路一致)

代码

#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <algorithm>
#include <stdlib.h>

using namespace std;
const int N = 100;
/*
*         7
       3      8
    8     1      0
  2    7     4        4
4    5     2      6       5


5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
30

*/
void trackBack(int track[][N], int tow[][N], int n)
{
	int i = 1,j=1;
	while (i <= n)
	{
		printf("%d->", tow[i][j]);
		if (track[i][j] == 1)
			;
		else
			j++;
		i++;
	}
}
int sumMax(int tow[][N], int n)
{
	int ans[N][N] = { 0 };
	int track[N][N];
	for (int i = 1; i <= n; i++)
		ans[n][i] = tow[n][i];//答案最后一层赋值为塔的最后一层,往上赋值为左右最大值与中间相加
	for (int i = n - 1; i >= 1; i--)
		for (int j = 1; j <= i; j++)
			if (ans[i + 1][j] > ans[i + 1][j+1])
			{
				track[i][j] = 1;//左边
				ans[i][j] = ans[i + 1][j] + tow[i][j];
			}
			else
			{
				track[i][j] = 2;//右边
				ans[i][j] = ans[i + 1][j+1]+tow[i][j];
			}
	trackBack(track, tow, n);
	return ans[1][1];
}
int main()
{
	int n;
	cout << "Please enter the The number of floors of the Number tower:" << endl;
	int towel[N][N] = { 0 };
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= i; j++)
			cin >> towel[i][j];
	int max=sumMax(towel, n);
	cout << "The MaxSum is " << max;
	return 0;
}

测试案例

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

运行结果

时间复杂度分析

速记

dp[i][j]=max(dp[i][j],dp[i][j+1])+a[i][j] 

回溯函数留意一下

同时内层循环参数留意一下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熟人看不到

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值