HDU 2084 数塔(动态规划)

时间:2023-02-03 16:16:06

数塔

http://acm.hdu.edu.cn/showproblem.php?pid=2084

Problem Description
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
HDU 2084 数塔(动态规划)
已经告诉你了,这是个DP的题目,你能AC吗?

 
Input
输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。
 
Output
对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。
 
Sample Input
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
 
Sample Output
30
 

动态方程:dp[i][j] = max(dp[i-1][j] + map[i][j], dp[i-1][j-1] + map[i][j]);

解题代码:

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std; const int MAX = ; int dp[MAX][MAX], map[MAX][MAX]; int main ()
{
int n, T;
scanf("%d", &T);
int max_sum;
while (T--)
{
scanf ("%d", &n);
memset(map, , sizeof (map));
memset(dp, , sizeof (dp));
for (int i = ; i <= n; i ++)
{
for (int j = ; j <= i; j ++)
scanf ("%d", &map[i][j]);
}
max_sum = ;
for (int i = ; i <= n; i ++)
{
for (int j = ; j <= i; j ++)
{
dp[i][j] = max(dp[i-][j] + map[i][j], dp[i-][j-] + map[i][j]);
if (dp[i][j] > max_sum)
max_sum = dp[i][j];
}
}
printf ("%d\n", max_sum);
}
return ;
}

HDU 2084 数塔(动态规划)的更多相关文章

  1. hdu 2084 数塔&lpar;动态规划&rpar;

    本题是一个经典的动态规划题. 直接利用记忆化搜索:见图解 Ac code : #include<stdio.h> #include<string.h> #define max( ...

  2. hdu 2084 数塔 &lpar;简单dp&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=2084 数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory L ...

  3. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  4. HDU 2084 数塔 &lpar;DP&rpar;

    数塔 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pr ...

  5. HDU 2084 数塔&lpar;简单DP入门&rpar;

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  6. ACM 杭电HDU 2084 数塔 &lbrack;解题报告&rsqb;

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. HDU 2084 数塔 (dp)

    题目链接 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数 ...

  8. HDU 2084 数塔 (动态规划DP)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 题目分析:此题采用动态规划自底向上计算,如果我们要知道所走之和最大,那么最后一步肯定是走最后一排 ...

  9. HDU 2084 数塔

    没什么好说的,这是我学DP的第一道题目. //#define LOCAL #include <iostream> #include <cstdio> #include < ...

随机推荐

  1. 网络流 最大流HDU 3549

    //////////在这幅图中我们首先要增广1->2->4->6,这时可以获得一个容量为2的流,但是如果不建立4->2反向弧的话,则无法进一步增广,最终答案为2,显然是不对的, ...

  2. HDU 5007 Post Robot KMP &lpar;ICPC西安赛区网络预选赛 1001&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 解题报告:输入一篇文章,从头开始,当遇到 “Apple”, “iPhone”, “iPod”, ...

  3. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  4. pdf嵌入字体

    论文提交时,要求所有的字体都是嵌入的,为这个问题折腾了很久,发现了一个很好的答案,记一下: http://*.com/questions/4231656/how-do-i-em ...

  5. CentOS 7 安装无线驱动

    一.确认网卡的版本 lspci | grep Network [root@bogon ~]# lspci | grep Network :) :) [root@bogon ~]# 二.下载网卡的驱动, ...

  6. git plumbing 更加底层命令解析-深入理解GIT

    原文: http://rypress.com/tutorials/git/plumbing 本文详细介绍GIT Plumbing--更加底层的git命令,你将会对git在内部是如何管理和呈现一个项目r ...

  7. &lbrack;Angular 2&rsqb; &commat;Input &amp&semi; &commat;Output Event with ref

    The application is simple, to build a color picker: When click the rect box, it will check the color ...

  8. 转:靠谱的代码和DRY

    http://www.cppblog.com/vczh/archive/2014/07/15/207658.html 靠谱的代码和DRY 上次有人来要求我写一篇文章谈谈什么代码才是好代码,是谁我已经忘 ...

  9. Loading Data into a Table;MySQL从本地向数据库导入数据

    在localhost中准备好了一个test数据库和一个pet表: mysql> SHOW DATABASES; +--------------------+ | Database | +---- ...

  10. NumPy库

    NumPy详细教程(官网手册翻译) Python之Numpy详细教程 一.基础篇 1.NumPy - Ndarray 对象 ndarray描述相同类型的元素集合, 可以使用基于零的索引访问集合中的项目 ...