码迷,mamicode.com
首页 > 其他好文 > 详细

(LightOJ 1004) Monkey Banana Problem 简单dp

时间:2016-08-15 14:34:40      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

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 (≤ 50), denoting the number of test cases.

Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.

Output
For each case, print the case number and maximum number of bananas eaten by the monkey.

Sample Input
Output for Sample Input
2
4
7
6 4
2 5 10
9 8 12 2
2 12 7
8 2
10
2
1
2 3
1
Case 1: 63
Case 2: 5

 题意 从上到下,使和最大

#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=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
                scanf("%d",&a[i][j]);
        }
        for(int i=n+1; i<2*n; i++)
        {
            for(int j=1; j<=2*n-i; j++)
                scanf("%d",&a[i][j]);
        }
        met(dp,0);
        dp[1][1]=a[1][1];
        for(int i=2; i<=n; i++)
        {
            for(int j=1; j<=i; j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a[i][j]);
                dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i][j]);
            }
        }
        for(int i=n+1; i<2*n; i++)
        {
            for(int j=1; j<=2*n-i; j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i][j]);
                dp[i][j]=max(dp[i][j],dp[i-1][j+1]+a[i][j]);
            }
        }
        printf("Case %d: %d\n",con++,dp[2*n-1][1]);
    }
    return 0;
}

 

(LightOJ 1004) Monkey Banana Problem 简单dp

标签:

原文地址:http://www.cnblogs.com/jun939026567/p/5772715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!