博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(LightOJ 1004) Monkey Banana Problem 简单dp
阅读量:5046 次
发布时间:2019-06-12

本文共 2041 字,大约阅读时间需要 6 分钟。

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.InputInput 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.OutputFor each case, print the case number and maximum number of bananas eaten by the monkey.Sample InputOutput for Sample Input2476 42 5 109 8 12 22 12 78 210212 31Case 1: 63Case 2: 5

 题意 从上到下,使和最大

#include 
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define INF 0x3f3f3f3f#define met(a,b) memset(a,b,sizeof(a));#define N 511using 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;}

 

转载于:https://www.cnblogs.com/jun939026567/p/5772715.html

你可能感兴趣的文章
201521123069 《Java程序设计》 第4周学习总结
查看>>
线性表的顺序存储——线性表的本质和操作
查看>>
【linux】重置fedora root密码
查看>>
用swing做一个简单的正则验证工具
查看>>
百度坐标(BD-09)、国测局坐标(火星坐标,GCJ-02)和WGS-84坐标互转
查看>>
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>
爬虫综合大作业
查看>>
Kubernetes 运维学习笔记
查看>>
并查集 经典 畅通工程
查看>>
Spark MLlib 之 Naive Bayes
查看>>
php修改SESSION的有效生存时间
查看>>
spring security 11种过滤器介绍
查看>>
Hibernate一对多、多对一关联
查看>>
一、记录Git使用中遇到的问题及解决方法
查看>>
学习网址
查看>>
前端表格插件datatables
查看>>
内部类
查看>>
树链剖分入门
查看>>
图解算法时间复杂度
查看>>