动态规划(1)数字三角形
2017-03-03 15:53
232 查看
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. InputYour 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.OutputYour 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 5Sample Output
30
#include<stdlib.h> #include<iostream> #include<algorithm> #define MAX 101 using namespace std; int D[MAX][MAX]; int maxsum[MAX][MAX]; int ans; int n; int main() { int i,j; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=i;j++){ cin>>D[i][j]; } for(i=1;i<=n;i++) for(j=1;j<=i;j++) { if(j==1) maxsum[i][j]=maxsum[i-1][j]+D[i][j]; else if(j==i) maxsum[i][j]=maxsum[i-1][j-1]+D[i][j]; else maxsum[i][j]=max(maxsum[i-1][j],maxsum[i-1][j-1])+D[i][j]; } ans=0; for(j=1;j<=n;j++) ans=max(ans,maxsum [j]); printf("%d",ans); system("pause"); }
相关文章推荐
- 数字三角形(动态规划)poj1163
- 【动态规划】动态规划解题的一般思路——以数字三角形为例
- 动态规划详解(数字三角形POJ1163)
- |Tyvj|动态规划|P1044 数字三角形
- POJ1163 The Triangle(数字三角形) (动态规划初步)
- 数字三角形升级版(棋盘型动态规划)
- 数字三角形 动态规划
- 【动态规划】数字三角形问题
- 数字三角形--动态规划
- POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)
- [动态规划]数字三角形、最长上升子序列
- 动态规划--数字三角形问题
- hiho#1037 : 数字三角形 (动态规划)
- 动态规划--再论(数字三角形 poj1163)
- hdu 1176 免费馅饼(动态规划 数字三角形问题变形)
- SDUT_数字三角形问题_动态规划
- 数字三角形-动态规划-无
- poj 1163数字三角形问题--动态规划
- 动态规划 问题之数字三角形(倒序递推)
- 【转】数字三角形-递推-动态规划