(LightOJ 1004) Monkey Banana Problem 简单dp

You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.

Input
Input starts with an integer T (≤ ), denoting the number of test cases. Every case starts with an integer N ( ≤ N ≤ ). It denotes that, there will be *N - rows. The ith ( ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - lines. The jth ( ≤ j < N) line contains N - j integers. Each number is greater than zero and less than . Output
For each case, print the case number and maximum number of bananas eaten by the monkey. Sample Input
Output for Sample Input Case :
Case :

题意 从上到下,使和最大

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include <math.h>
#include<queue>
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
#define N 511
using namespace std;
int dp[N][N],a[N][N];
int main()
{
int t,n,con=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
{
for(int j=; j<=i; j++)
scanf("%d",&a[i][j]);
}
for(int i=n+; i<*n; i++)
{
for(int j=; j<=*n-i; j++)
scanf("%d",&a[i][j]);
}
met(dp,);
dp[][]=a[][];
for(int i=; i<=n; i++)
{
for(int j=; j<=i; j++)
{
dp[i][j]=max(dp[i][j],dp[i-][j-]+a[i][j]);
dp[i][j]=max(dp[i][j],dp[i-][j]+a[i][j]);
}
}
for(int i=n+; i<*n; i++)
{
for(int j=; j<=*n-i; j++)
{
dp[i][j]=max(dp[i][j],dp[i-][j]+a[i][j]);
dp[i][j]=max(dp[i][j],dp[i-][j+]+a[i][j]);
}
}
printf("Case %d: %d\n",con++,dp[*n-][]);
}
return ;
}
上一篇:【互动问答分享】第15期决胜云计算大数据时代Spark亚太研究院公益大讲堂


下一篇:转:浅谈CSS在前端优化中一些值得注意的关键点