博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
暑假集训每日一题0718(动规)
阅读量:4978 次
发布时间:2019-06-12

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

Description

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 cellsdown 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 ≤ 1000). 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 2^15.

Output

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

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

Sample Output

Case 1: 63
Case 2: 5
 
典型的动态规划题,跟数字三角形类似。
由于内存比较卡,所以要用滚动数组优化。
View Code
#include 
#include
#define N 1005#define MAX(a,b) ((a)>(b)?(a):(b))int a[2][N];int main(){ int t,n,i,j,id,kase=0; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); scanf("%d",&n); id=0; for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { scanf("%d",&a[id][j]); } for(j=1;j<=i;j++) { a[id][j]+=MAX(a[id^1][j-1],a[id^1][j]); } id^=1; } for(i=n-1;i>0;i--) { for(j=1;j<=i;j++) { scanf("%d",&a[id][j]); } for(j=1;j<=i;j++) { a[id][j]+=MAX(a[id^1][j],a[id^1][j+1]); } id^=1; } printf("Case %d: %d\n",++kase,a[id^1][1]); } return 0;}

 

转载于:https://www.cnblogs.com/algorithms/archive/2012/07/18/2597729.html

你可能感兴趣的文章
服务器常用词汇表
查看>>
显示成本还原
查看>>
广度优先搜索算法与双向广度优先搜索算法
查看>>
Python函数式编程指南(三):迭代器
查看>>
aix的lvm管理
查看>>
《深入理解Android 卷1》笔记
查看>>
静态成员实例
查看>>
第十二章 执行期别辩识
查看>>
win10多用户远程登录
查看>>
JSP基础知识
查看>>
R语言合并data.frame
查看>>
质量保证与质量控制的区别
查看>>
异常点检测算法
查看>>
回文数 (Palindrome Numbers,Dhaka 2003,LA 2889)
查看>>
深入理解Java虚拟机
查看>>
Excel解析工具POI
查看>>
jQuery----blur()方法
查看>>
FFmpeg命令详解
查看>>
`ln`命令创建软链接错误
查看>>
mysqlDOS命令
查看>>