Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 50122   Accepted: 30285

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


题意就是三角形走左下角或右上角和max,从下往上找,动态规划。
 
代码:
 
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=+;
int main(){
int n,a[N][N];
while(~scanf("%d",&n)){
memset(a,,sizeof(a));
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++)
a[i][j]+=max(a[i+][j],a[i+][j+]);
}
printf("%d\n",a[][]);
}
return ;
}
												

POJ1163-The Triangle-动态规划的更多相关文章

  1. Poj1163 The Triangle(动态规划求最大权值的路径)

    一.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 pro ...

  2. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  3. POJ1163——The Triangle

    Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program t ...

  4. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  5. (数字三角形)POJ1163 The Triangle

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

  6. 120. Triangle(动态规划 三角形最小路径 难 想)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. POJ1163 The Triangle

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44997   Accepted: 27174 Description 73 ...

  8. nyoj 18-The Triangle(动态规划)

    18-The Triangle 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:5 submit:5 题目描述: 7 3 8 8 1 0 2 7 4 ...

  9. poj1163The Triangle(动态规划,记忆化搜索)

    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 calc ...

  10. poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)

    经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...

随机推荐

  1. linux集群批量执行命令

    因为工作需要,需要修改集群中机器的配置,一台一台的修改太浪费时间,就想能不能通过自动化脚本批量执行命令,尝试写一个,自己shell不熟悉,写的有点渣渣 if [ "$#" -ne ...

  2. Lucene分词停用词库stopwords

    ! " $ % & ' ( ) * + , - -- . .. ... ...... ................... ./ .一 .数 .日 / // 0 1 2 3 4 5 ...

  3. VS访问不到TFS、VS连接TFS报TF30063

    =============================================== 20170704_第一次修改                       ccb_warlock === ...

  4. 通过js中的useragrent来判断设备是pc端还是移动端,跳转不同的地址

    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...

  5. pyshark 得到payload

    mydata = pkt[okt.highest_layer].data mydata.decode("hex")

  6. 树上操作[HAOI 2015]

    树链剖分裸题: 树剖点这里:传送门 代码: #include<bits/stdc++.h> #define sight(c) ('0'<=c&&c<='9') ...

  7. leetcode — linked-list-cycle

    /** * Source : https://oj.leetcode.com/problems/linked-list-cycle/ * * Given a linked list, determin ...

  8. 分布式监控系统Zabbix3.2添加自动发现磁盘IO并注册监控

     zabbix并没有给我们提供这么一个模板来完成在Linux中磁盘IO的监控,所以我们需要自己来创建一个,在此还是在Linux OS中添加. 由于一台服务器中磁盘众多,如果只一两台可以手动添加,但服务 ...

  9. swift4.0 正则表达式判断手机号

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Menlo; color: #ffffff; background-color: #282b3 ...

  10. NOTIC: Invalid argument supplied for foreach()

    NOTIC: [2] Invalid argument supplied for foreach() Warning: Invalid argument supplied for foreach() ...