The Triangle
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59698   Accepted: 35792

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

DP水题,记忆化递归和递推都行,其状态转换式为 dp[i][j] = a[i][j] + max{d[i+1][j],d[i+1][j+1]};

C++代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
int a[maxn][maxn];
int dp[maxn][maxn];
int main(){
int N;
scanf("%d",&N);
memset(a,,sizeof(a));
for(int i = ; i <= N; i++){
for(int j = ; j <= i; j++)
cin>>a[i][j];
}
memset(dp,-,sizeof(dp));
for(int i = ; i <= N; i++)
dp[N][i] = a[N][i];
for(int i = N-; i >= ;i--){
for(int j = ; j <= i; j++)
dp[i][j] = max(dp[i+][j],dp[i+][j+]) + a[i][j];
}
printf("%d\n",dp[][]);
return ;
}

(数字三角形)POJ1163 The Triangle的更多相关文章

  1. 动态规划略有所得 数字三角形(POJ1163)

    在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 三角形的行数大于1小于等于100,数 ...

  2. 动态规划之数字三角形(POJ1163)

    在下面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 既然求目标问题是根据查表得来的,自然 ...

  3. 动态规划入门——数字三角形(Java)

    动态规划的概念对于新手来说枯燥难懂,就算看懂了,做题的时候依旧抓耳挠腮的毫无头绪,这些比较难理解的算法,还是需要根据例子来一步步学习和理解,从而熟练掌握,下面,咱们就通过一个简单的小例子来学习动态规划 ...

  4. dp递推 数字三角形,dp初学者概念总结

    数字三角形(POJ1163)          在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出 ...

  5. 数字三角形 · Triangle

    从上到下用DP. [抄题]: 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4, ...

  6. lintcode:数字三角形

    题目: 数字三角形 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 样例 比如,给出下列数字三角形: [      [2],     [3,4],    [6 ...

  7. DP入门(1)——数字三角形问题

    一.问题描述 如上图所示,有一个由非负整数组成的三角形,第一行只有一个数,除了最下行之外每个数的左下方和右下方各有一个数.现请你在此数字三角形中寻找一条从首行到最下行的路径,使得路径上所经过的数字之和 ...

  8. Problem C: 动态规划基础题目之数字三角形

    Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 208  Solved: 139[Submit][Sta ...

  9. G:数字三角形

    总时间限制: 1000ms 内存限制: 65536kB描述73   88   1   02   7   4   44   5   2   6   5 (图1) 图1给出了一个数字三角形.从三角形的顶部 ...

  10. 4829 [DP]数字三角形升级版

    4829 [DP]数字三角形升级版  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 从数字三角形的顶部(如图, ...

随机推荐

  1. Pulse-per-second (PPS) Signal Interfacing

    Some radio clocks and related timekeeping gear have a pulse-per-second (PPS) signal that can be used ...

  2. How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1

    Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...

  3. Codeforces1037G A Game on Strings 【SG函数】【区间DP】

    题目分析: 一开始没想到SG函数,其它想到了就开始敲,后来发现不对才发现了需要SG函数. 把每个字母单独提出来,可以发现有用的区间只有两个字母之间的区间和一个位置到另一个字母的不跨越另一个相同字母的位 ...

  4. python3,打印一年的某一天是一年的第几天

    year = int(input('year:')) month = int(input('month:')) day = int(input('day:')) months = (0,31,59,9 ...

  5. day5 模拟用户登录

    _user= "yangtuo" _passwd = " # passd_authentication = False #flag 标志位 for i in range( ...

  6. linux tar 解压命令

    如果提示 common not find 先进行安装如下 wget http://www.rarsoft.com/rar/rarlinux-5.3.0.tar.gz tar -zxvf rarlinu ...

  7. Android stadio 生成项目 Plugin with id 'com.android.application' not found

    buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2. ...

  8. 正睿 2019 省选附加赛 Day10

    A 核心就是一个公式 \[\sum_{i = 0}^{k} S(k, i) \tbinom{x}{i} i\] S是第二类斯特林数 递推公式 \(S_2(n,k)=S_2(n−1,k−1)+kS_2( ...

  9. Android应用开发资源

    Android应用设计和开发人员现在可以参考由Android用户体验(UX)团队官方发布的Android设计指南.该指南提供了开发者应该遵循的基本原则,并列出了很多细节指导,涉及设备与显示.主题.触控 ...

  10. 【BZOJ4316】小C的独立集(动态规划)

    [BZOJ4316]小C的独立集(动态规划) 题面 BZOJ 题解 考虑树的独立集求法 设\(f[i][0/1]\)表示\(i\)这个点一定不选,以及\(i\)这个点无所谓的最大值 转移\(f[u][ ...