数塔 Medium
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 mcolumns. 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<cstring>
#include<cstdio> using namespace std;
const int N = + ;
int mat[N][N], a[N][N], b[N][N], c[N][N], d[N][N]; int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) scanf("%d", &mat[i][j]); for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) a[i][j] = max(a[i-][j], a[i][j-]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = m; j > ; j--) b[i][j] = max(b[i+][j], b[i][j+]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = ; j <= m; j++) c[i][j] = max(c[i+][j], c[i][j-]) + mat[i][j]; for(int i = ; i <= n; i++)
for(int j = m; j > ; j--) d[i][j] = max(d[i-][j], d[i][j+]) + mat[i][j];
int ans = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
ans = max(ans, a[i-][j] + b[i+][j] + c[i][j-] + d[i][j+]),
ans = max(ans, a[i][j-] + b[i][j+] + c[i+][j] + d[i-][j]);
printf("%d\n", ans);
}
最新文章
- mac office 激活
- python操作mysql数据库
- jquery学习——遍历
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Gym 100827G Number Game (博弈)
- python使用PIL压缩图片
- Win7系统安装MySQL5.5.21图解教程
- Leetcode题解(28)
- Smarty基础用法
- oracle pl/sql中的循环及if语句
- 【Luogu1876】开灯(数论)
- python爬虫动态html selenium.webdriver
- 使用node.js 脚手架搭建Vue项目
- FUTABA 13-ST-84GINK + DS3231 时钟
- 【Mysql】常用命令
- c# async和await 用法(阻塞与不阻塞)
- vue router history模式开发ngnix配置
- 关于CSS3的filter(滤镜) 属性
- SVN的Branch和Tag管理
- java static方法不能被重写@Override