The Triangle

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49955   Accepted: 30177

Description

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers 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.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

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

Sample Output

30

Source

题目要求:

输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线。
规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个。
题目分析:

典型的动态规划。

因此我们可以从下往上推,相邻的两个数中找较大的与上层相加,得出的结果相邻的两个数中再找较大的与上层相加,以此类推。用二维数组d_[][]记录从下到该点的最大值。

核心代码

d[i][j] += d[i+1][j] > d[i+1][j+1] ? d[i+1][j] : d[i+1][j+1];

最后的结果就是d[0][0]。

下面给出AC代码:

 #include <stdio.h>
#include <string.h>
int max(int a,int b)
{
return a>b?a:b;
}
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
int a[][],d[][];
int n;
int dp(int i,int j)
{
if(d[i][j]>=)
return d[i][j];
return d[i][j]=a[i][j]+(i==n-?:max(dp(i+,j),dp(i+,j+)));
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
for(j=;j<i+;j++)
scanf("%d",&a[i][j]);
memset(d,-,sizeof(d));
printf("%d\n",dp(,));
}
return ;
}

POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】的更多相关文章

  1. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  2. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  5. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. [noip2016]组合数问题<dp+杨辉三角>

    题目链接:https://vijos.org/p/2006 当时在考场上只想到了暴力的做法,现在自己看了以后还是没思路,最后看大佬说的杨辉三角才懂这题... 我自己总结了一下,我不能反应出杨辉三角的递 ...

  7. poj 1163 The Triangle(dp)

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43993   Accepted: 26553 De ...

  8. 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  9. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

随机推荐

  1. 查看windows、linux的SN

     gwmi win32_bios    [root@live-al-ops-pxe-2 ~]# dmidecode | grep Number | sed -n '1p' Serial Number: ...

  2. ArcGIS API for JavaScript 4.2学习笔记[17] 官方第七章Searching(空间查询)概览与解释

    空间分析和空间查询是WebGIS有别于其他Web平台的特点.到这一章,就开始步入空间分析的内容了. [Search widget] 介绍空间查询的核心小部件"Search". [S ...

  3. BZOJ2001 HNOI2010 城市建设

    题目大意:动态最小生成树,可以离线,每次修改后回答,点数20000,边和修改都是50000. 顾昱洲是真的神:顾昱洲_浅谈一类分治算法 链接: https://pan.baidu.com/s/1c2l ...

  4. Extjs 取消backspace事件

    Ext.getDoc().on('keydown',function(e){ if(e.getKey() == 8 && e.getTarget().type =='text' &am ...

  5. IE下判断IE版本的语句...[if lte IE 8]……[endif]

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   <!--[if lte IE 7]> <![endif]--> ...

  6. python内置函数与匿名函数

    内置函数 Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() d ...

  7. jmeter测试

    时间过得飞快,转眼间就到了公司半个月了,这是第三周上班,从上班到现在感觉自己什么都没有做,只是写了一些前台的验证,况且我的前台并不是很熟,js学了很久也快忘记了,看了看插件也不咋会用,但是自己也写了点 ...

  8. this与base关键字

    this关键字 this关键字代表当前对象,通过this关键字可以访问当前对象的成员.(当前对象的成员:自己本身的成员+从父类继承过来的所有的成员.) this关键字可以访问:本类的所有成员和父类的非 ...

  9. MicroPython教程之TPYBoard开发板DIY红外寻迹小车

    智能小车现在差不多是电子竞赛或者DIY中的主流了,寻迹,壁障,遥控什么的,相信大家也都见得很多了,这次就大家探讨一下寻迹小车的制作方法,不同于以往的是这次的程序不用C语言写,而是要使用python语言 ...

  10. JS 对象API之判断自有属性、共有属性

    自有属性:对象实例私有的属性,只有该对象实例可用 共有属性:对象实例共有的属性,所有对象实例都可用 要判断对象实例的自有属性.共有属性:首先看看JS给我们提供的两个方法: 1.判断是否是对象实例的属性 ...