Light OJ 1004
来源:互联网 发布:网络扫描仪怎么设置 编辑:程序博客网 时间:2024/06/11 08:32
Light OJ 1004 - Monkey Banana Problem
纯属个人理解 不是最佳算法
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 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.
For each case, print the case number and maximum number of bananas eaten by the monkey.
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
这道题是道动态规划的问题,题意是,一只猴子从最上边跳到最下边,途经过的数字和最大,要求 只能 斜着往下跳;
上半部分我直接用的a【i】【j】保存的最大值,由题意直到 a[i][j] =max(a[i-1][j-1],a[i-1][j])+a[i][j]; 我将a[i][j]给了dp[j];用于下半部分的求解
下半部分dp[j]=max(dp[j],dp[j-1])+a[i][j];最后输出dp[1]就是所求
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int a[300][300];
int dp[300];
int t,n;
int main()
{
int q=1;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
int j=1;
int i;
//输入上半部分
for(i=1;j<=n;i++)
{
scanf("%d",&a[j][i]);
if(i==j)
{
j++;
i=0;
}
}
j=n+1;
int k=n-1;
//输入下半部分
for(i=1;j<=2*n-1;i++)
{
scanf("%d",&a[j][i]);
if(j==n)
{
dp[i]=a[j][i];
}
if(i==k)
{
j++;
k--;
i=0;
}
}
dp[1]=a[1][1];//防止就1的格子
//上半部分的最大值
for(i=2;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=max(a[i-1][j-1],a[i-1][j])+a[i][j];
dp[j]=a[i][j];
}
}
//下半部分的最大值
for(i=n+1;i<=2*n-1;i++)
{
for(j=1;j<=n;j++)
{
dp[j]=max(dp[j],dp[j+1])+a[i][j];
}
}
printf("Case %d: %d\n",q++,dp[1]);
}
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
- Light OJ 1004
- Light OJ 1004
- light oj 1004 水DP
- light oj
- light oj
- Light OJ
- Light OJ
- 【DP】 Light OJ 1004 Monkey Banana Problem
- light oj 1004 - Monkey Banana Problem
- light OJ 1004(简单递推)
- Light oj 1004 - Monkey Banana Problem(DP)
- Light OJ 1000
- Light OJ 1001
- Light OJ 1008
- Light OJ 1022
- Light OJ 1015
- Light OJ 1042
- light oj 1128
- HBase知识体系探究与整理
- 2. Add Two Numbers
- 7_27C语言|二维数组的sizeof
- html5语义化标签
- python网络爬虫 百度贴吧
- Light OJ 1004
- 搜索二叉树---之狸猫换太子的删除方法
- [leetcode]468. Validate IP Address
- opencv0-入门必知
- 浏览器不能下载response输出的excel
- 深度学习笔记----softmax
- Centos 安装 Tomcat
- django数据库错误相关问题
- 用内存池管理c++对象