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.

题目大意:两个人(假设为A,B),打算健身,有N行M列个房间,每个房间能消耗Map[i][j]的卡路里,A起点为(1,1)要达到(n,m)点,且每次只能向右走一步或向下走一步,B起点为(n,1),要达到(1,m),且每次只能向上走一步,或向右走一步。有要求A,B必须在某一个房间相遇一次,且A,B在该房间不再消耗卡路里,因为两人锻炼身体的速度不同,所以在相遇时经过的房间数亦可能不相同。问两人合计最多消耗多少卡路里。

题目思路:分别从四个角向对角打dp表,然后遍历各个点认为该点为相遇的房间,依据该点求四个dp式的和,并找出最大值,具体看代码。

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; long long dp1[MAX][MAX],dp2[MAX][MAX],dp3[MAX][MAX],dp4[MAX][MAX],ans; int Map[MAX][MAX],n,m; void Ans()
{
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
memset(dp4,,sizeof(dp4));
int i,j;
for(i=; i<=n; i++)//dp1
{
for(j=; j<=m; j++)
{
dp1[i][j]=Map[i][j]+max(dp1[i-][j],dp1[i][j-]);
}
} for(i=n; i>=; i--)//dp2
{
for(j=m; j>=; j--)
{
dp2[i][j]=Map[i][j]+max(dp2[i+][j],dp2[i][j+]);
}
} for(i=; i<=n; i++)//dp3
{
for(j=m; j>=; j--)
{
dp3[i][j]=Map[i][j]+max(dp3[i-][j],dp3[i][j+]);
}
}
for(i=n; i>=; i--)//dp4
{
for(j=; j<=m; j++)
{
dp4[i][j]=Map[i][j]+max(dp4[i+][j],dp4[i][j-]);
}
}
} int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=;
for(i=; i<=n; i++)
for(j=; j<=m; j++)
scanf("%d",&Map[i][j]);
Ans();
for(i=; i<n; i++)//因为只能相遇一次,如果在第1行,第n行,第1列,第m列相遇的话那么下一步两人将走入同一个房间或对方之前进入过的房间
{
for(j=; j<m; j++)
{
ans = max(ans,dp1[i-][j]+dp2[i+][j]+dp3[i][j+]+dp4[i][j-]);//因为相遇的房间不计入卡路里,所以将该点的四个角的dp式相加
ans = max(ans,dp1[i][j-]+dp2[i][j+]+dp3[i-][j]+dp4[i+][j]);
}
} printf("%lld\n",ans);
}
return ;
}

CodeForces 429B Working out 动态规划的更多相关文章

  1. CodeForces 429B Working out DP

    E - Working out Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  2. CODEFORCES 429B 动态规划

    http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...

  3. Codeforces 839C Journey - 树形动态规划 - 数学期望

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...

  4. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  5. Codeforces 837D Round Subset - 动态规划 - 数论

    Let's call the roundness of the number the number of zeros to which it ends. You have an array of n ...

  6. Codeforces 429B Working out

    http://codeforces.com/contest/429/problem/B 题意:一个从左下到右上,一个从左上到右下,要求只相交一次,求整个路径和的最大值 思路:发现可以枚举交点,然后算到 ...

  7. CodeForces 623E Transforming Sequence 动态规划 倍增 多项式 FFT 组合数学

    原文链接http://www.cnblogs.com/zhouzhendong/p/8848990.html 题目传送门 - CodeForces 623E 题意 给定$n,k$. 让你构造序列$a( ...

  8. Codeforces 101623E English Restaurant - 动态规划

    题目传送门 传送门 题目大意 餐厅有$n$张桌子,第$i$张桌子可以容纳$c_i$个人,有$t$组客人,每组客人的人数等概率是$[1, g]$中的整数. 每来一组人数为$x$客人,餐厅如果能找到最小的 ...

  9. Codeforces 264C Choosing Balls 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html 题目传送门 - CF264C 题意 给定一个有 $n$ 个元素的序列,序列的每一个元素是个 ...

随机推荐

  1. 13.hibernate的native sql查询(转自xiaoluo501395377)

    hibernate的native sql查询   在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hi ...

  2. 基于C++的顺序表的实现

    顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...

  3. Android上的远程调试

    来源: http://www.seejs.com/archives/296 目录 远程调试概述 使用 Chrome 的 ADB 扩展进行远程调试 1. 安装 ADB 扩展 2. 启用你的移动设备上的 ...

  4. drupal中安装CKEditor文本编辑器,并配置图片上传功能

    一.下载: 1.CKEditor模块 2.IMCE模块 二.安装       1.复制: 下载完上面两个模块之后,解压,将解压后整个文件夹,复制粘贴,放到 sites\all\modules下面,个人 ...

  5. MySql 安装报错 :Last Error:Unable to update security. Access denied for user 'root'@'localhost(useing password:YES)

    在网上查了一下,其实这个问题很好解决,. try again 然后current password  mysql是默认密码为空,不要填,记住不要填就ok了

  6. 软件测试之α测试和Beta测试

    实施验收测试的常用策略有三种,它们分别是: · 正式验收 · 非正式验收或Alpha 测试 · Beta 测试 因此,Alpha测试和Beta测试都属于验收测试.所谓验收测试是软件产品完成了功能测试和 ...

  7. Zend Studio配合Xdebug调试

    以下配置均在windows环境下. 1.下载xdebug 使用phpinfo()查看当前php的版本信息: 到xdebug下载页面下载对应的xdebug版本:   将解压出的php_xdebug-2. ...

  8. Apache+Tomcat服务器集群配置

    在实际应用中,如果网站的访问量很大,为了提高访问速度,可以与多个Tomcat服务器与Apache服务器集成,让他们共同运行servlet/jsp 组件的任务,多个Tomcat服务器构成了一个集群(Cl ...

  9. 常用的css

    产品鼠标经过加边框效果 .productsCol:hover { box-shadow: 0 0 0 3px #333333 inset; transition: all 0.2s ease 0s; ...

  10. MySQL查看索引、表信息、触发器

    查看索引: select * FROM information_schema.TABLE_CONSTRAINTS ; select * FROM information_schema.TABLE_CO ...