CodeForces 429B Working out 动态规划
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 动态规划的更多相关文章
- CodeForces 429B Working out DP
E - Working out Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- CODEFORCES 429B 动态规划
http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...
- Codeforces 839C Journey - 树形动态规划 - 数学期望
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- 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 ...
- Codeforces 429B Working out
http://codeforces.com/contest/429/problem/B 题意:一个从左下到右上,一个从左上到右下,要求只相交一次,求整个路径和的最大值 思路:发现可以枚举交点,然后算到 ...
- CodeForces 623E Transforming Sequence 动态规划 倍增 多项式 FFT 组合数学
原文链接http://www.cnblogs.com/zhouzhendong/p/8848990.html 题目传送门 - CodeForces 623E 题意 给定$n,k$. 让你构造序列$a( ...
- Codeforces 101623E English Restaurant - 动态规划
题目传送门 传送门 题目大意 餐厅有$n$张桌子,第$i$张桌子可以容纳$c_i$个人,有$t$组客人,每组客人的人数等概率是$[1, g]$中的整数. 每来一组人数为$x$客人,餐厅如果能找到最小的 ...
- Codeforces 264C Choosing Balls 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html 题目传送门 - CF264C 题意 给定一个有 $n$ 个元素的序列,序列的每一个元素是个 ...
随机推荐
- 如何使用cygwin去编译cocos2dx项目中的C++文件
将生成的cocos2dx的Android项目导入到eclipse 可以先测试一下如何编译C++项目: 1.打开cygwin,进入到Android项目对应的目录下面去: 2.编译脚本 在编译脚本之间,如 ...
- linshi_temp_erweima_html
<!doctype html><html><head><meta charset="utf-8"><meta content= ...
- POJ 1042 Gone Fishing#贪心
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...
- Java 反射 分析类和对象
Java 反射 分析类和对象 @author ixenos 摘要:优化程序启动策略.在运行时使用反射分析类的结构和对象 优化程序启动策略 在启动时,包含main方法的类被加载.它会加载所有它需要的类. ...
- Openjudge-计算概论(A)-过滤多余的空格
描述: 一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格. 输入一行,一个字符串(长度不超过200),句子的头和尾都没有空格.输出过滤之后的句子. 样例输入 Hello world.Th ...
- poj2386
湖计数描述由于最近的降雨,水汇集在不同的地方,在农民约翰的领域,这是代表一个长方形的N×M(1μ= 100:1 = M = 100)平方.每一方都包含水(’w')或干燥的土地(“.”).农民约翰想弄清 ...
- mac xmind快捷键
tab:新建分支 command +z : 撤销 command + "+":放大 command + "-":缩小 shift + enter : 文字换行
- JMeter基础
转载自虫师-http://www.cnblogs.com/fnng/archive/2012/12/21/2828440.html JMeter 介绍: 一个非常优秀的开源的性能测试工具. 优点:你用 ...
- mybatis框架源码学习
转:来自https://my.oschina.net/u/1458864/blog/293659 摘要:初始化mybatis,所有的配置都在configuation 对象中使用mybatis,从sql ...
- js去除字符串中所有html标签及 符号
近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...