LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

1004 - Monkey Banana Problem

PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimensional array and can jump in any of the adjacent cellsdown fromits current position (see figure). While
moving from one cell to another, themonkey eats all the bananas kept in that cell. The monkey enters into the arrayfrom the upper part and goes out through the lower part. Find the maximumnumber 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 be2*N - 1 rows. The ith(1 ≤ i ≤ N) line of nextN lines contains exactlyinumbers. Then
there will beN - 1 lines. The jth (1≤ j < N) line containsN - j integers. Each number isgreater than zero and less than 215.

Output

For each case, print the case number and maximum number ofbananas 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

Note

Dataset is huge, use faster I/O methods.

题目链接:http://lightoj.com/volume_showproblem.php?problem=1004

题目大意:求从上往下路径的最大权值。

解题思路:数字三角形的变形,把菱形分为上下两部分求即可。dp[i][j]表示路径到第i行第j个点的最大权值和。

首先输入正三角,j=1时:dp[i][j] = dp[i-1][j];

j=n时:dp[i][j] = dp[i-1][j-1];

j=2~n-1: dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);

然后输入倒三角,dp[i][j]=max(dp[i-1][j],dp[i-1][j+1]);

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
int a[102][102],dp[102][102];
int main()
{
    int i,j,t,n,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            for(j=1;j<=i;j++)
            scanf("%d",&a[i][j]);
        dp[1][1]=a[1][1];
        for(i=2;i<=n;i++)
            for(j=1;j<=i;j++)
            {
                if(j==1)
                    dp[i][j]=a[i][j]+dp[i-1][j];
                else if(j==n)
                    dp[i][j]=a[i][j]+dp[i-1][j-1];
                else
                    dp[i][j]=a[i][j]+max(dp[i-1][j-1],dp[i-1][j]);
            }
        for(i=n-1;i>=1;i--)
            for(j=1;j<=i;j++)
                scanf("%d",&a[i][j]);
        for(i=n-1;i>=1;i--)
            for(j=1;j<=i;j++)
                dp[i][j]=a[i][j]+max(dp[i+1][j],dp[i+1][j+1]);
        printf("Case %d: %d\n",++cnt,dp[1][1]);
    }
    return 0;
}
时间: 2024-10-15 19:08:54

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)的相关文章

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 dim

Lightoj 1004 - Monkey Banana Problem

1004 - Monkey Banana Problem    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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

LightOJ 1004 - Monkey Banana Problem 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1004 题意:两个数塔相接,求最大的路径和. 解法:简单DP. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <

LightOJ 1047 Neighbor House (DP 数字三角形变形)

1047 - Neighbor House PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB The people of Mohammadpur have decided to paint each oftheir houses red, green, or blue. They've also decided that no two neighboringhouses will be pai

(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). Wh

F - Monkey Banana Problem

F - Monkey Banana Problem Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond s

4829 [DP]数字三角形升级版

4829 [DP]数字三角形升级版 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 从数字三角形的顶部(如图,第一行的5表示行数)到底部有很多条不同的路径.对于每条路径,把路径上面的数加起来可以得到一个和,且!!!!!!!!! ================================================================================== =================

[LightOJ1004]Monkey Banana Problem(dp)

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1004 题意:数塔的变形,上面一个下面一个,看清楚了直接做就行了. 上半部分转移方程dp(i,j)=max(dp(i-1,j),dp(i-1,j+1))+G(i,j) 下半部分转移方程dp(i,j)=max(dp(i-1,j-1),dp(i-1,j))+G(i,j) 1 #include <algorithm> 2 #include <io

HDU1176:免费馅饼(dp,数字三角形的应用)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1176 这题就是数字三角行的变形,可惜对于我这个渣渣来说就是没发现,区别是他可以保持在三个点,他左边的点,右边的点,还有原点, 从下往上处理.其他就没有什么好说的了,注意一下细节问题,我记得这题我白白贡献了几次WA. #include <iostream> #include <stdio.h> #include <string.h> #include <math.h>