Light oj-1004 - Monkey Banana Problem,数字三角形的变形版~

                                                                                                 1004 - Monkey Banana Problem
Time Limit: 2 second(s)Memory Limit: 32 MB
link

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

Note

Dataset is huge, use faster I/O methods.


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

   说白了这道题就是两个数字三角形,一个正的,一个倒过来的;倒着的做法与数字三角形一样,只需要处理正的;

   我们来看样例,把这个分为上下两部分,下面就是个倒的数字三角形,正的三角形其实与倒的处理方式类似,我们做数字三角形是从下往上推,或者从上往下推,如果两种都会的话这道题没什么问题了;

   博主其实做这个以前都是从下往上推的,但这题分为两部分就不好处理了,所以我选择正三角形从上往下推,而倒三角形相当于一个正的三角形从下往上推,做法不就和纯的数字三角形一模一样了;若不懂来看代码;

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//const int INF=0x3f3f3f3f;
const int N=200+10;//开二维数组足够了,没有用到空间压缩;
long long a[N][N];
int main()
{
    int t,n,i,j;
    memset(a,0,sizeof(a));
    scanf("%d",&t);
    int t1=t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=1; i<=2*n-1; i++)
        {
            if(i<=n)
            {
                for(j=1; j<=i; j++)
                    scanf("%lld",&a[i][j]);//正三角形;
            }
            else
            {
                for(j=1; j<=2*n-i; j++)//倒三角;
                    scanf("%lld",&a[i][j]);
            }
        }
        printf("Case %d: ",t1-t);
        for(i=2; i<=n; i++)
            for(j=1; j<=i; j++)
                a[i][j]+=max(a[i-1][j-1],a[i-1][j]);//从上往下,状态转移方程;
        for(i=n+1; i<=2*n-1; i++)
            for(j=1; j<=2*n-i; j++)
                a[i][j]+=max(a[i-1][j],a[i-1][j+1]);//状态转移方程几乎一样
        printf("%lld\n",a[2*n-1][1]);注意格式,博主用I64dPEI
    }
    return 0;
}


转载于:https://www.cnblogs.com/nyist-TC-LYQ/p/7208234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SDUT-OJ(Software Development University of Tsinghua Online Judge)是一个在线编程平台,提供给清华大学软件学院的学生和爱好者练习和解决算法问题的环境,其中包括各种计算机科学题目,包括数据结构、算法、图形等。对于"最小生成树"(Minimum Spanning Tree, MST)问题,它是图论中的经典问题,目标是从一个加权无向图中找到一棵包含所有顶点的树,使得树的所有边的权重之和最小。 在C语言中,最常见的是使用Prim算法或Kruskal算法来求解最小生成树。Prim算法从一个顶点开始,逐步添加与当前生成树相连且权重最小的边,直到所有顶点都被包含;而Kruskal算法则是从小到大对所有边排序,每次选取没有形成环的新边加入到树中。 如果你想了解如何用C语言实现这些算法,这里简单概括一下: - 通常使用优先队列(堆)来存储边和它们的权重,以便快速查找最小值。 - 从任意一个顶点开始,遍历与其相邻的边,若新边不形成环,就更新树,并将新边加入优先队列。 - Kruskal算法: - 先将所有的边按照权重升序排序。 - 创建一个空的最小生成树,然后依次取出排序后的边,如果这条边连接的两个顶点不在同一个连通分量,则将其添加到树中。 如果你需要更详细的代码示例,或者有具体的问题想了解(比如如何处理环、如何实现优先队列等),请告诉我,我会为你提供相应的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值