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. Microsoft Visual Studio 2012旗舰版(VS2012中文版下载)官方中文版

    Microsoft Visual Studio 2012 Ultimate旗舰版(VS2012中文版下载)是一个最先进的开发解决方案,它使各种规模的团队能够设计和创建出使用户欣喜的引人注目的应用程序. ...

  2. MySQL数据库入门(建库和建表)--陈远波

    建库.建表 1.建库 (1)SQL语句命令建库: Create database数据库名称  (该方法创建的数据库没有设置编码乱码) 1 2 3 4 5 -- 创建数据库时,设置数据库的编码方式 -- ...

  3. MVCC的一些理解

    link 一.MVCC简介 MVCC (Multiversion Concurrency Control),即多版本并发控制技术,它使得大部分支持行锁的事务引擎,不再单纯的使用行锁来进行数据库的并发控 ...

  4. shell脚本异步日志分析-接口耗时、可用率

    背景:现有日志接入日志报表大盘,为了避免作业高峰期间(双十一),系统也要观测系统整体情况,因此提出了观测近五分钟,接口成功率以及耗时等工具(默认统计最近五分钟,并进行结果汇总统计) 使用说明 前提:p ...

  5. Python 阿里大于发送手机验证码

    1.安装阿里大于的包 pip install top 2.事例脚本 # -*- coding: utf-8 -*- import top.api appkey = '2353xxxx' secret ...

  6. NPOI 1.2 教程

    NPOI 1.2 教程官方地址 很多人可能对NPOI还很陌生,别担心,通过本教程你将对NPOI有进一步的认识和理解. 目录 1. 认识NPOI 2. 使用NPOI生成xls文件 2.1 创建基本内容 ...

  7. java多线程(六)-线程的状态和常用的方法

    一个线程可以处于以下几种状态之一: (1) 新建(new):当线程被创建时,它只会短暂的处于这种状态,此时它已经获得了必须的系统资源,并执行了初始化,该线程已经有资格获取cpu时间了,之后它将转化为可 ...

  8. Linux重启后raid5的名字发生变化

    Linux重启后raid5的名字发生变化 使用raid,每次重启后,都会变换设备路径 比如原来为/dev/md0 重启一次变成了/dev/md127 这个问题,可以使用修改配置文件来解决. 1.mda ...

  9. python 列表赋值和列表 sort 方法注意的问题

    列表赋值 >>> a = b = [] >>> a.append() >>> a [] >>> b [] >>> ...

  10. Linux系统使用-CentOS7 for Redis

    Redis系列(一):CentOS系统安装与环境配置 1.为什么使用虚拟机和CentOS 最近Redis比较 热门而且易于使用 而 Redisd对window支持并不好. 引用官方说明:http:// ...