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 <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

int t, n;
int dp[220][220];
int p[220][220];

int main()
{
    scanf("%d",&t);
    for (int ca = 1; ca <= t;ca++)
    {
        scanf("%d",&n);
        int tmp = n - 1;
        int tmp2 = n - 1;

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                scanf("%d",&p[i][j]);
        for (int i = n + 1; i <= 2 * n - 1; i++)
            for (int j = 1; j <= 2 * n - i; j++)
                scanf("%d", &p[i][j]);

        memset(dp,0,sizeof(dp));

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + p[i][j];

        for (int i = n + 1; i <= 2 * n - 1; i++)
            for (int j = 1; j <= 2 * n - i; j++)
                dp[i][j] = max(dp[i-1][j],dp[i-1][j+1])+p[i][j];

        printf("Case %d: %d\n",ca,dp[2*n - 1][1]);
    }
    return 0;
}
时间: 2024-10-15 08:02:21

LightOJ 1004 - Monkey Banana Problem 【DP】的相关文章

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 twodimen

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

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 简单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

LightOJ 1231 - Coin Change (I) 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1231 题意:多重部分和的解法有几种. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional&

[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

ZOJ1093 Monkey and Banana 【DP】

一.题目 ZOJ 1093 二.题目源程序 #include <stdio.h>//一个箱子有3种h..所以总共有3*n种高度.按面积从大到小排序 #include <stdlib.h> struct block { int x,y,z,h; }a[200]; int cmp(const void *a,const void *b)//快排,模版 { return ((struct block *)b)->x*((struct block *)b)->y - ((str

HDOJ1176 免费馅饼 【DP】+【经典数塔】

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23986    Accepted Submission(s): 8093 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的1

HDOJ4540 威威猫系列故事——打地鼠 【DP】

威威猫系列故事--打地鼠 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1445    Accepted Submission(s): 713 Problem Description 威威猫最近不务正业,每天沉迷于游戏"打地鼠". 每当朋友们劝他别太着迷游戏,应该好好工作的时候,他总是说,我是威威猫,猫打老鼠就是我的工作! 无话