Number Triangles

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

PROGRAM NAME: numtri

INPUT FORMAT

The first line contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

SAMPLE INPUT (file numtri.in)

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

OUTPUT FORMAT

A single line containing the largest sum using the traversal specified.

SAMPLE OUTPUT (file numtri.out)

30

题目大意:数字三角形,动态规划(DP)入门题目,说的是有一个一群数字排列成三角形,你现在站在上顶点,现在想要走到底边,每次你可以选择走左下或是右下,每次踩到的数字都累加起来,求最大累加和。
思路:这题搜索不好搞,复杂度2^1000简直开玩笑,仔细思索,发现如果我知道脚下的两个点哪个更优,我就有唯一的行走策略了,那就从下往上推,f[i][j]表示从这点出发到达底边的最大累加和,他可以更新它左上的和右上的,从下往上推一遍答案就出来了,就是f[1][1];代码见下面
 /*
ID:fffgrdcc1
PROB:numtri
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[][],f[][];
int main()
{
freopen("numtri.in","r",stdin);
freopen("numtri.out","w",stdout);
memset(f,,sizeof(f));
memset(a,,sizeof(a));
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=n+;i>;i--)
{
for(int j=;j<=i;j++)
{
f[i-][j]=max(f[i-][j],f[i][j]+a[i-][j]);
f[i-][j-]=max(f[i-][j-],f[i][j]+a[i-][j-]);
}
}
printf("%d\n",f[][]);
return ;
}

(这题原来写过,所以写的略快,边界的处理比较粗糙,不过因为我提前把边界外一层留了出来且都设为0,所以不会影响答案)

USACO 1.5 Number Triangles的更多相关文章

  1. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. USACO training course Number Triangles 数塔 /// DP oj10122

    题目大意: ...就是数塔       7         3   8      8   1   0     2   7   4   4 4   5   2   6   5 7+3+8+7+5=30 ...

  3. 洛谷——P1216 [USACO1.5]数字三角形 Number Triangles

    P1216 [USACO1.5]数字三角形 Number Triangles 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左 ...

  4. 【洛谷 P1216】【IOI1994】【USACO1.5】数字三角形 Number Triangles

    (如此多的标签qaq) 数字三角形 Number Triangles[传送门] 本来打算当DP练的,没想到写着写着成递推了(汗) 好的没有时间了,我们附个ac代码(改天不写): #include< ...

  5. P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles

    P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles 这个题吧,之前学DP的时候就做过一次了,其实还是挺简单的,如果一步一步按照找状态定义,找边界条件,找转移 ...

  6. USACO Section 1.5 Number Triangles 解题报告

    题目 题目描述 现在有一个数字三角形,第一行有一个数字,第二行有两个数字,以此类推...,现在从第一行开始累加,每次在一个节点累加完之后,下一个节点必须是它的左下方的那个节点或者是右下方那个节点,一直 ...

  7. Number Triangles

    题目描述 Consider the number triangle shown below. Write a program that calculates the highest sum of nu ...

  8. P1216 [USACO1.5]数字三角形 Number Triangles

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

  9. AC日记——[USACO1.5]数字三角形 Number Triangles 洛谷 P1216

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

随机推荐

  1. Spring《七》ApplicationContext

    1.国际化支持 getMessage()提供了国际化支持. Bean中必须定义为messageSource. <bean id="messageSource" class=& ...

  2. let,const,var三者之间的区别

    在ES6中新增了两种定义变量的命令let和const,在这之前相信大家都对var定义变量很熟悉,那么在了解ES6方法前, 1.我们先来回顾一下var定义变量的方法. 下面来看这段代码: for (va ...

  3. Codeforces Round #454

    Masha and Bears Tic-Tac-Toe Shockers Seating of Students Party Power Tower Reverses

  4. vuejs开发H5页面总结

    最近参与了APP内嵌H5页面的开发,这次使用vuejs替代了jQuery,仅仅把vuejs当做一个库来使用,效率提高之外代码可读性更强,在此分享一下自己的一些开发中总结的经验. 关于布局方案 当拿到设 ...

  5. javascript学习中自己对作用域和作用域链理解

    在javascript学习中作用域和作用域链还是相对难理解些,下面我关于javascript作用域和作用域链做一下详细介绍,给各位初学者答疑解惑. 首先我们介绍一下什么是作用域?  从字面上理解就是起 ...

  6. 统计学——Excel实现单(双)因素方差分析

    笔记链接:http://www.cnblogs.com/igoslly/p/6784206.html 加载Excel“数据分析”工具包 [文件]→[选项]→[加载项]→[Excel加载项]→[转到] ...

  7. [翻译]内存一致性模型 --- memory consistency model

    I will just give the analogy with which I understand memory consistency models (or memory models, fo ...

  8. hiho1804 - 整数分解、组合数、乘法逆元

    题目链接 题目叙述很啰嗦,可以简化为:n个球[1-1e5],放到m个不同的桶里,一共多少种不同的放法.[桶里可以不放] ---------------------------------------- ...

  9. 脚本_部署LNMP平台

    #!bin/bash#功能:部署LNMP平台,实际运行脚本时,需要去除备注.#作者:liusingbonfunction menu {                //定义函数menu        ...

  10. form表单提交三种方式,demo实例详解

    第一种:使用type=submit  可以直接提交 <html> <head> <title>submit直接提交</title> </head& ...