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. HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods

    注:本文为个人学习摘录,原文地址为:http://javaeedevelop.iteye.com/blog/1725299 An HTTP request is a class consisting ...

  2. LeetCode OJ 48. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. (转载)app ico图标字体制作

    图标字体化浅谈   在做手机端Web App项目中,经常会遇到小图标在手机上显示比较模糊的问题,经过实践发现了一种比较好的解决方案,图标字体化.在微社区项目中,有很多小的Icon(图标),如分享.回复 ...

  4. Myeclipse 激活代码 8.6以前的版本

    public class Akey { private static final String LL = "Decompiling this copyrighted software is ...

  5. linux 非root用户 ssh 免密码登录

    之所以要把这个记录下来 是因为它的确和root用户不一样root用户 不需要改动什么权限问题  只要生成私钥/公钥对 即可 但是一样的操作在普通用户上就出了问题了 折腾了老半天 ssh-keygen ...

  6. Python 学习笔记7

    今天很残酷,明天很残酷,后天很美好.但绝大多数人会死在明天的路上.只有真正的勇士才能看到后天的太阳! Python学习是枯燥的.但是一定要坚持! 昨天学习了数据结构和模块. 今天学习输入和输出.错误与 ...

  7. __doPostBack初识

    周五在公司看到有看到Request.Params["__EVENTARGUMENT"]的代码不解,不解遂上网查找,发现一篇文章<Understanding the JavaS ...

  8. ssh environment variable

    1 down vote When you run a command as an argument to ssh, the command is run directly by sshd; the s ...

  9. java类中为什么设置set和get方法操作属性

    java程序规范中会建议大家尽量将类中的属性私有化,即定义为private变量,通过设置set和get函数来对属性进行操作.一些人存在这样的疑问,为什么不直接将属性设置为public,以后调用属性时直 ...

  10. java 图形界面

    1.创建一个窗口框架 /** * java 用户界面框架 * 2016/5/10 */ package org.windows; import javax.swing.*; public class ...