poj1163 The Triangle

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
int n;
int d[100][100];
int map[100][100];
void dp(int i,int j)
{
    if(i==n-1)
    {
        d[i][j]=0;
        return;
    }
    if(d[i+1][j]==-1)
    {
        dp(i+1,j);
    }
    if(d[i+1][j+1]==-1)
    {
        dp(i+1,j+1);
    }
    d[i][j]=0;
    if(d[i][j]==-1||d[i][j]<d[i+1][j]+map[i+1][j])
    {
        d[i][j]=d[i+1][j]+map[i+1][j];
    }
    if(d[i][j]==-1||d[i][j]<d[i+1][j+1]+map[i+1][j+1])
    {
        d[i][j]=d[i+1][j+1]+map[i+1][j+1];
    }
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<=i;j++)
        {
            scanf("%d",&map[i][j]);
            d[i][j]=-1;
        }
    }
    dp(0,0);
    printf("%d\n",d[0][0]+map[0][0]);
    return 0;
}

//200K 0MS

posted @ 2012-08-04 18:04  willzhang  阅读(153)  评论(0)    收藏  举报