POJ 1163 The Triangle DP题解

寻找路径,动态规划法题解。

本题和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题解

时间: 2024-07-30 10:19:21

POJ 1163 The Triangle DP题解的相关文章

poj 1163 The Triangle(dp)

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43993   Accepted: 26553 Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on

递推DP POJ 1163 The Triangle

题目传送门 1 /* 2 数塔 3 自底向上 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const

poj 1163 The Triangle &amp;poj 3167 Cow Bowling (dp)

链接:poj 1163 题意:输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个. 状态方程:f[i][j]=max(f[i-1][j-1],f[i-1][j])+a[i][j]; 1163代码: #include<stdio.h> #include<string.h> int a[105][105],f[105][105]; int max(int a,int b)

POJ 1163 The Triangle (简单线性dp)

OJ题目 : click here~~ 题目分析:给一个数字三角形,从最上面一个数字开始,方向只能往左下或者右下,一直到最后一行,求经过的所有数字和的最大值. 搞清楚在输入的数据中,route的方向就行. AC_CODE int num[102][102]; int main(){ int n , i , j , k ; while(cin >> n){ int x[102][102]; for(i = 1;i <= n;i++) for(j = 1;j <= i;j++) sca

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <

POJ 1163 The Triangle

题目链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39022   Accepted: 23430 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 calculat

POJ 1163 The Triangle (简单 DP 数字的最大路线和)

题目大意:从三角形顶部数字走,每次只能走到这个数字的左下角或者右下角的数字,直到底部,计算走过的线路的数字之和,求这个和的最大值. #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 105; int dp[MAXN][MAXN], a[MAXN][MAXN]; int main() { int n; while( cin>&g

poj 1163 The Triangle (动态规划)

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37778   Accepted: 22685 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