POJ 1163 The Triangle 简单DP

题目来源:http://poj.org/problem?id=1163

题意:数塔问题,经典DP

思路:从上向下走,每次都走最大路径,在最下面一行循环找最大即可。

代码:

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

#define CLR(arr,val) memset(arr,val,sizeof(val))
const int N =110;
int num[N][N],dp[N][N];
int max(int a,int b){
	return a>b?a:b;
}
int main(){
	//freopen("1.txt","r",stdin);
	int n;
	while(scanf("%d",&n) != EOF){
		CLR(num,0);
		for(int i = 1;i <= n;++i){
		  for(int j = 1;j <= i;++j)
			  scanf("%d",&num[i][j]);
		}
		CLR(dp,0);
	    dp[1][1] = num[1][1];
		for(int i = 1;i <= n;++i){
			for(int j = 1;j <= i;++j){
			  dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + num[i][j];
			}
		}
		int mmax = 0;
		for(int i = 1;i <= n;++i)
			if(mmax < dp[n][i])
				mmax = dp[n][i];
		printf("%d\n",mmax);
	}
	return 0;
}


你可能感兴趣的:(POJ 1163 The Triangle 简单DP)