数塔

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

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

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

已经告诉你了,这是个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 数塔(动态规划)

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

  2. hdu 2084 数塔 (简单dp)

    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 数塔 (DP)

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

  5. HDU 2084 数塔(简单DP入门)

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

  6. ACM 杭电HDU 2084 数塔 [解题报告]

    数塔 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. LightOJ 1317 第八次比赛 A 题

    Description You probably have played the game "Throwing Balls into the Basket". It is a si ...

  2. window7部署solr 4.7

    环境:win7 + tomcat 7.0.50 + solr 4.7 备注:C:\solr-4.7.0为solr.zip解压后的目录 C:\apache-tomcat-7.0.50为tomcat目录 ...

  3. oracle自定义job名字,job调度

    一.调试创建 begin -- create_schedule dbms_scheduler.create_schedule(schedule_name => 's_change_send_da ...

  4. hdu 4150 Powerful Incantation

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...

  5. hdu 5273 Dylans loves sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...

  6. OpenStack:安装Keystone

    >安装Keystone1. 安装# apt-get install keystone2. 创建dbcreate database keystone;grant all privileges on ...

  7. ExtJs4学习MVC中的Store

    Ext.data.Store是extjs中用来进行数据交换和数据交互的标准中间件,无论是Grid还是ComboBox,都是通过它实现数据读取.类型转换.排序分页和搜索等操作的. 1 2 3 4 5 6 ...

  8. 常用gradle命令

    1.build.gradle ext { profile = "dev" tag='web' if (project.hasProperty('pro')) { temp = pr ...

  9. 第2章 变量和基本类型 附3---底层const和顶层const

    和英文版的对: As we’ve seen, a pointer is an object that can point to a different object. As a result,we c ...

  10. UIScrowView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...