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. Tuple Class

    Inheritance Hierarchy   System.Object  System.Tuple Methods   Name Description Create<T1>(T1) ...

  2. linux系统命令大全

    文件管理 cat chattr chgrp chmod chown cksum cmp cp cut diff diffstat file find git gitview in indent les ...

  3. codeforces1093G Multidimensional Queries 【线段树】

    题目分析: 搜索2^k种情况,线段树分别处理就行了,正确性明显. 代码: #include<bits/stdc++.h> using namespace std; ; int n,k; ] ...

  4. CF 670C Cinema(算竞进阶习题)

    离散化+排序 离散化统计人数就好,本来不难,但是测试点太丧心病狂了...CF还是大哥啊 #include <bits/stdc++.h> #define INF 0x3f3f3f3f us ...

  5. python中可变参数和关键字参数总结

    #_*_coding='utf-8' #可变参数 def person(name,age,*args): #定义了可变参数args print('传入的名字为:',name) print('传入的年龄 ...

  6. Linux block(1k) block(4k) 换算 gb

    输入 df  显示1k blocks  大小   再输入  df -h  显示 gb换算大小  结论 block(1k) 计算公式为:  block(1k)   /1024/1000  = xx gb ...

  7. Hdoj 2289.Cup 题解

    Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we kn ...

  8. Java抽象类简单学习

    使用抽象类应该注意的几个要点: 包含一个或者多个抽象方法的类必须被声明为抽象类. 将类声明为抽象类,不一定含有抽象方法. 通常认为,在抽象类中不应该包括具体方法,建议尽量将通用的域和方法放在超类中. ...

  9. emwin之错误使用控件函数导致死机现象

    @2018-10-15 导致死机的代码示例如下 /** * @brief widget ID define * @{ */ #define ID_WINDOW_0 (GUI_ID_USER + 0x0 ...

  10. AWS免费EC2

    终于弄到这个EC2了.回想下过程大概如下 1.注册账号 https://amazonaws-china.com/cn/free/ 可以这地址点击"创建免费账户" 2.注册填写信息, ...