Working out
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.
Example
3 3
100 100 100
100 1 100
100 100 100
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].
求出四个角到每个点的最大消耗,枚举每个点求出最合适的。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#define Max 1001
using namespace std; int mp[][];
int dp1[][],dp2[][],dp3[][],dp4[][];
int ma = ;
int main()
{
int n,m;
cin>>n>>m;
for(int i = ;i <= n;i ++)for(int j = ;j <= m;j ++)cin>>mp[i][j];
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 = m;j >= ;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 = ;j <= m;j ++)dp4[i][j] = max(dp4[i+][j],dp4[i][j-])+mp[i][j];
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= m;j ++)
ma = max(ma,max(dp1[i][j-]+dp2[i][j+]+dp3[i+][j]+dp4[i-][j],dp1[i-][j]+dp2[i+][j]+dp3[i][j+]+dp4[i][j-]));
}
cout<<ma;
}
随机推荐
- codeforces27D Ring Road 2
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- ubuntu install git vim Plug manage
在UBUNTU采用163或是阿里云来更新源,最新的更新源地址可以在网上查阅, 阿里源 deb http://mirrors.aliyun.com/ubuntu/ bionic main restric ...
- 前端构建大法 Gulp 系列
参考: 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp ...
- 英语词根与单词的说文解字---词根示例1、第10页 st(at)
英语词根与单词的说文解字---词根示例1.第10页 st(at) 一.总结 一句话总结: 站 state,establish,constitution 英 [ɪ'stæblɪʃ; e-] 美 [ɪˈ ...
- JAVA常用数据结构API
Quene
- C语言调用DIRECT3D的实例代码,通过lpVtbl字段进行
m_pDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION); int w = 1920; int h = 1080; D3DPRESENT_PARAMETER ...
- mail_location not set and autodetection failed 解决方案[devecot, sendmail]
安装dovecot比较简单, 但是也需要配置, 如果不进行任何配置时,在测试时会出现如下的提示: dovecot: pop3(wwufengg): Error: user wwufengg: Init ...
- commons-fileupload实现上传进度条的显示
本文将使用 apache fileupload ,spring MVC jquery 实现一个带进度条的多文件上传, 由于fileupload 的局限,暂不能实现每个上传文件都显示进度条, ...
- 015——数组(十五)sort natsort shuffle natcasesoft array_multisort
<?php /*数组排序函数 * sort natsort shuffle natcasesoft array_multisort */ //sort() 对数组元素进行递增的排序, /*$ar ...
- vue 侧边导航栏递归显示
import axios from "axios"; import tabs1 from "../tab_content/tab1.vue"; import m ...