标签:blog io for 2014 line amp log leetcode
寻找路径,动态规划法题解。
本题和Leetcode的triangle题目差不多一样的,本题要求的是找到最大路径和。
逆向思维,从底往上查找起就可以了。
因为从上往下可以扩展到很多路径,而从下往上个点的路径是由两条缩减到一条。
这样就可以很简单记录最大路径了。
#include <stdio.h> const short MAX_ROW = 101; short triangle[MAX_ROW][MAX_ROW]; short table[MAX_ROW]; short row; inline short max(short a, short b) { return a > b ? a : b; } short getMaxSum() { for (short i = 0; i < row; i++) table[i] = triangle[row-1][i]; for (row-=2; row >= 0; row--) { for (short i = 0; i <= row; i++) { table[i] = triangle[row][i] + max(table[i], table[i+1]); } } return table[0]; } int main() { while (~scanf("%d", &row)) { for (short i = 0; i < row; i++) { for (short j = 0; j <= i; j++) { scanf("%d", &triangle[i][j]); } } printf("%d\n", getMaxSum()); } return 0; }
POJ 1163 The Triangle DP题解,布布扣,bubuko.com
标签:blog io for 2014 line amp log leetcode
原文地址:http://blog.csdn.net/kenden23/article/details/38397023