B. Working out
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 the i-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 workout a[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.

Examples
input
Copy
3 3
100 100 100
100 1 100
100 100 100
output
800
Note
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

题意:给出n*m的方格,a从左上到右下,b从左下到右下,a只能往右往下走,b只能往右往上走,减去重合的一个交叉点值,求a和b走过的路径权值最大值

题解:分析交叉点的位置 肯定不在四个边上,否则会存在多个交叉点,当a b位于交叉点,只有俩种情况:

1) A向右走,相遇后继续向右走,而B向上走,相遇后继续向上走 
2) A向下走,相遇后继续向下走,而B向右走,相遇后继续向右走

然后枚举四个角的dp值

最后max求最大值

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
int a[maxn][maxn];
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int dp3[maxn][maxn];
int dp4[maxn][maxn];
int main()
{
int n,m;
while(cin>>n>>m){
if(n==||m==)
{
break;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp3));
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-])+a[i][j];//左上方
}
}
for(int i=n;i>=;i--){
for(int j=m;j>=;j--){
dp2[i][j] = max(dp2[i+][j],dp2[i][j+])+a[i][j];//右下方
}
}
for(int i=n;i>=;i--){
for(int j=;j<=m;j++){
dp3[i][j] = max(dp3[i+][j],dp3[i][j-])+a[i][j];//左下方
}
}
for(int i=;i<=n;i++){
for(int j=m;j>=;j--){
dp4[i][j] = max(dp4[i-][j],dp4[i][j+])+a[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]);//左 右 上 下
}
}
cout<<ans<<endl;
}
return ;
}

CF 429B B.Working out 四个角递推的更多相关文章

  1. Code Force 429B Working out【递推dp】

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  2. Android 二维码扫描框 加四个角及中间横线自动下滑

    红色为加四个角  黄色为扫描线自动下滑 /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Ver ...

  3. unity3d 获取相机视口四个角的坐标

    功能:如标题所示,主要考虑用来做3d Plane的自适应屏幕 /// <summary> /// 获取指定距离下相机视口四个角的坐标 /// </summary> /// &l ...

  4. [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...

  5. Bootstrap3基础 img-rounded 图片的四个角改成圆角

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  6. HTML和JS完成页面点击四个角弹出管理页面

    实现方法1: HTML代码: <div class="top-left-corner"></div> <div class="top-rig ...

  7. 四角递推(CF Working out,动态规划递推)

    题目:假如有A,B两个人,在一个m*n的矩阵,然后A在(1,1),B在(m,1),A要走到(m,n),B要走到(1,n),两人走的过程中可以捡起格子上的数字,而且两人速度不一样,可以同时到一个点(哪怕 ...

  8. cf 429B Working out(简单dp)

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  9. GMap获取可视范围内四个角的坐标

    原理: 先获取控件的四个顶点,逐一将其转换成经纬度坐标. private void GetBonds() { //左上↖ PointLatLng pLeftTop = map1.FromLocalTo ...

随机推荐

  1. Spring Data MongoDB 四:基本文档改动(update)(一)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 二:MongoDB加入.删除.改动 一.简单介绍 Spring Data  Mo ...

  2. [学些东西]用爬虫练习网站来练习burp suite

    最近看爬虫的内容.刚好看到黑板客爬虫第二关http://www.heibanke.com/lesson/crawler_ex01. ADO的i春秋课程里面提到的.另外推荐学习爬虫的好书<web ...

  3. 从架构层面杜绝lua中使用未定义的变量

    # 从架构层面杜绝lua中使用未定义的变量 标签(空格分隔): lua --- lua中有一个很坑的地方:1.就是如果一个变量拼写错误,会自动的认为你定义了一个值为nil的全局变量.2.如果在func ...

  4. wifi 协议栈的历史的总结

    google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...

  5. Block浅析一

    1.在Block结构体中含有isa指针,这就证明了Block其实就是对象,并具有一般对象的所有功能. 2.Block是OC中的一种数据类型,在iOS开发中被广泛使用. 3.block的应用 (1)遍历 ...

  6. Vue.js的动态组件模板

    组件并不总是具有相同的结构.有时需要管理许多不同的状态.异步执行此操作会很有帮助. 实例: 组件模板某些网页中用于多个位置,例如通知,注释和附件.让我们来一起看一下评论,看一下我表达的意思是什么.评论 ...

  7. h5的缓存机制

    H5的缓存,大概有localstorage.sessionstorage.cookie和manifest. 一.LocalStorage LocalStorage是永久性的本地缓存,存储在客户端的浏览 ...

  8. 浏览器端JS导出EXCEL

    浏览器端JS导出EXCEL FileSaver.js 实现了在本身不支持 HTML5 W3C saveAs() FileSaver 接口的浏览器支持文件保存.FileSaver.js 在客户端保存文件 ...

  9. C#中XML解析的增加修改和删除

    01添加xml节点    private void AddXml(string image, string title)       {          XmlDocument xmlDoc = n ...

  10. JS错题整改

    获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div&quo ...