Description

Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in thei-th line and the j-th column.

Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workouta[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].

There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.

If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.

Input

The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).

Output

The output contains a single number — the maximum total gain possible.

Sample Input

Input
3 3 100 100 100 100 1 100 100 100 100
Output
800

代码:注意一点,交点不能在边界,如果在边界不满足两人只相遇一次的条件。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int mp[][];
int dp1[][],dp2[][],dp3[][],dp4[][]; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&mp[i][j]);
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
memset(dp4,,sizeof(dp4));
for(int i=; i<=n; i++)//右下
for(int j=; j<=m; j++)
dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+mp[i][j];
for(int i=n; i>=; i--)//右上
for(int j=; j<=m; j++)
dp2[i][j]=max(dp2[i+][j],dp2[i][j-])+mp[i][j];
for(int i=; i<=n; i++)//左下
for(int j=m; j>=; j--)
dp3[i][j]=max(dp3[i-][j],dp3[i][j+])+mp[i][j];
for(int i=n; i>=; i--)//左上
for(int j=m; j>=; j--)
dp4[i][j]=max(dp4[i+][j],dp4[i][j+])+mp[i][j];
int ans=;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
ans=max(ans,dp1[i-][j]+dp2[i][j-]+dp3[i][j+]+dp4[i+][j]);
ans=max(ans,dp1[i][j-]+dp2[i+][j]+dp3[i-][j]+dp4[i][j+]);
}
printf("%d\n",ans);
}
return ;
}

cf429B dp递推的更多相关文章

  1. hdu2089(数位DP 递推形式)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. hdu 2604 Queuing(dp递推)

    昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...

  3. Power oj2498/DP/递推

    power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3  ...

  4. BZOJ4321queue2——DP/递推

    题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行:  现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...

  5. Shell Necklace (dp递推改cdq分治 + fft)

    首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...

  6. hdu 1723 DP/递推

    题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...

  7. UVA 10559 Blocks(区间DP&&递推)

    题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...

  8. [NOI2009]管道取珠 DP + 递推

    ---题面--- 思路: 主要难点在思路的转化, 不能看见要求$\sum{a[i]^2}$就想着求a[i], 我们可以对其进行某种意义上的拆分,即a[i]实际上可以代表什么? 假设我们现在有两种取出某 ...

  9. HDU 2154 跳舞毯 | DP | 递推 | 规律

    Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...

随机推荐

  1. div+css进度条

    效果图: 进度条代码: <style type="text/css"> 红色:background-color:f05153:border:1px solid #f05 ...

  2. WP8图片缩放功能实现

    最近在学习WP8,想实现WP微信中查看图片时的放大缩小功能. 网上找了两个解决方案: 1 利用GestureListener 这个类在Microsoft.Phone.Controls.Toolkit中 ...

  3. SQL常用命令整理

    1.增加字段 alter table docdsp     add dspcodechar(200)2.删除字段     ALTER TABLE table_NAME DROP COLUMNcolum ...

  4. iOS 一个控件只能拥有一个父类

    不多说上图.多么惨痛的教训,一个子控件   只有最后的一个被addsubview才会生效.

  5. SQL Server output经典使用

    output经典使用 分类: sql2012-02-16 18:17 409人阅读 评论(0) 收藏 举报 outputinserttabledeletegonull OUTPUT是SQL SERVE ...

  6. !struct operator reload

    struct t3DObject //对象信息结构体{ int numOfVerts; // 模型中顶点的数目 int numOfFaces; // 模型中面的数目 int numTexVertex; ...

  7. 蜥蜴(bzoj 1066)

    Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平 ...

  8. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(三) 之 实现单聊,群聊,发送图片,文件。

    上篇讲解了如何搭建聊天服务器,以及客户端js怎么和layui的语法配合.服务器已经连接上了,那么聊天还会远吗? 进入正题,正如上一篇提到的我们用 Client.Group(groupId)的方法向客户 ...

  9. XMPP框架下微信项目总结(7)聊天通信处理-发送,接受数据

    前言:通其他的功能处理一样,聊天也是通过模块发起的成为:“消息模块” 原理:1 current客户端开启通过消息模块开启并监听消息(监听通过代理). 2 当“current客户端”收到来自“other ...

  10. 四、优化及调试--网站优化--SEO在网页制作中的应用

    SEO分类:白帽SEO.黑帽SEO 白帽SEO: 内容上的SEO: 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 前端SEO: 网站结构布局优化 扁平化结构(一 ...