cf 429B Working out(简单dp)
2 seconds
256 megabytes
standard input
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.
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).
The output contains a single number — the maximum total gain possible.
3 3
100 100 100
100 1 100
100 100 100
800
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从左下角走到右上角,路线交叉处的权值不算,问两条路线权值之和最大值。要求:两条路线只在一点交叉。
可以枚举交叉点,求该点到四个角落的权值之和。到每个角的权值都可以dp得到最大值。从左上角顺时针得到0,1,2,3四个方向。
两幅图的权值之和分别是: dp[i][j-1][0] + dp[i-1][j][1] + dp[i][j+1][2] + dp[i+1][j][3]
dp[i][j-1][3] + dp[i-1][j][0] + dp[i][j+1][1] + dp[i+1][j][2]
还有需注意的是,交叉点只在(n-2)*(m-2)里面这个矩形里变化(否则一个角上的矩形会不存在)
#include <bits/stdc++.h>
using namespace std; const int MAXN = ;
__int64 a[MAXN][MAXN];
__int64 dp[MAXN][MAXN][];//0, 1, 2, 3分别代表左上,右上,右下,左下 int main()
{ int n, m;
int i, j; while (~scanf("%d%d", &n, &m)) {
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
scanf("%I64d", &a[i][j]);
}
}
memset(dp, , sizeof(dp)); for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j - ][]) + a[i][j];
}
}
for (i = ; i <= n; ++i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j - ][]) + a[i][j];
}
} __int64 ans = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
}
}
printf("%I64d\n", ans);
} return ;
}
最新文章
- ajax提交数据到java后台,并且返回json格式数据前台接收处理值
- PHP 异常
- CEF3开发者系列之类和接口
- PHP5.3中关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择的问题
- IE6下z-index失效
- C#_delegate - example
- [NOIP2011]数的划分
- PHP操作Oracle数据库
- android隐式intent使用场景解析
- Android消息推送之GCM方式(一)
- xx-net 使用方式
- Dapper源码学习和源码修改(下篇)
- 关于$(function(){})的问题
- eclipse添加market ,maven
- JS的分号可以省掉吗?
- HTTP基础知识3
- elasticsearch(三) 之 elasticsearch目录介绍和配置文件详解
- Gitlab Issue Tracker and Wiki(二)
- 将asi-http-request引入到ARC工程需要做的 转
- BroadcastReceiver之实现锁屏、解锁样例