题目链接:http://codeforces.com/problemset/problem/429/B

给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右; 一个人从(n, 1) ->(1, m),只能向上或者向右。必须有一个相遇点, 相遇点的值不能被取到, 问两个人能得到的最大路径和是多少?

dp[i][j]:表示从一个点出发的最大值;先预处理从(1,1) (1,m) (n,1) (n,m)四个点出发的4个dp最大值。然后枚举所有的点,但是这个点不能在边缘,考虑枚举点不够,还要考虑枚举这个点的上下左右的值。

 #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + ;
int dp1[MAXN][MAXN] , dp2[MAXN][MAXN] , dp3[MAXN][MAXN] , dp4[MAXN][MAXN];
int a[MAXN][MAXN]; int main()
{
int n , m;
scanf("%d %d" , &n , &m);
for(int i = ; i <= n ; ++i) {
for(int j = ; j <= m ; ++j) {
scanf("%d" , &a[i][j]);
}
}
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 = ; j <= m ; ++j) {
dp2[i][j] = max(dp2[i + ][j] , dp2[i][j - ]) + a[i][j];
}
}
for(int i = ; i <= n ; ++i) {
for(int j = m ; j >= ; --j) {
dp3[i][j] = max(dp3[i][j + ] , dp3[i - ][j]) + a[i][j];
}
}
for(int i = n ; i >= ; --i) {
for(int j = m ; j >= ; --j) {
dp4[i][j] = max(dp4[i + ][j] , dp4[i][j + ]) + a[i][j];
}
}
int res = ;
for(int i = ; i < n ; ++i) {
for(int j = ; j < m ; ++j) {
res = max(res , dp1[i][j - ] + dp2[i + ][j] + dp3[i - ][j] + dp4[i][j + ]);
res = max(res , dp1[i - ][j] + dp2[i][j - ] + dp3[i][j + ] + dp4[i + ][j]);
}
}
printf("%d\n" , res);
return ;
}

Codeforces Round #245 (Div. 1) B. Working out (简单DP)的更多相关文章

  1. Codeforces Round #302 (Div. 2) C. Writing Code 简单dp

    C. Writing Code Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/prob ...

  2. Codeforces Round #245 (Div. 1) B. Working out (dp)

    题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...

  3. Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)

    题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  5. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  6. Codeforces Round #245 (Div. 1) B. Working out dp

    题目链接: http://codeforces.com/contest/429/problem/B B. Working out time limit per test2 secondsmemory ...

  7. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  8. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  9. Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心

    A. Points and Segments (easy) Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

随机推荐

  1. 函数rec_init_offsets

    http://database.51cto.com/art/201303/383042.htm /*************************************************** ...

  2. btr_pcur_t

    /** Persistent cursor */ typedef struct btr_pcur_struct btr_pcur_t; /* The persistent B-tree cursor ...

  3. 转载RabbitMQ入门(2)--工作队列

    工作队列 (使用Java客户端) 在这第一指南部分,我们写了通过同一命名的队列发送和接受消息.在这一部分,我们将会创建一个工作队列,在多个工作者之间使用分布式时间任务. 工作队列(亦称:任务队列)背后 ...

  4. liunx下mysql数据库使用之三范式,关系模型设计注意项,安装目录结构

    数据库的三范式第一范式===>每行记录的属性,是原子的,拆到不可拆为止.===>例如:一个人的籍贯,可以拆分为,省,市,县,乡,村 第二范式===>每行记录的非主属性(非主键属性), ...

  5. HDU 5734 Acperience

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. MyBatis 入门到精通(一) 了解MyBatis获取SqlSession

    MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...

  7. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  8. UINavigationController 操作记录

    //设置字体大小 颜色 { UIColor *color = [UIColor brownColor]; UIFont * font = [UIFont systemFontOfSize:20]; N ...

  9. 【windows核心编程】IO完成端口(IOCP)复制文件小例前简单说明

    1.关于IOCP IOCP即IO完成端口,是一种高伸缩高效率的异步IO方式,一个设备或文件与一个IO完成端口相关联,当文件或设备的异步IO操作完成的时候,去IO完成端口的[完成队列]取一项,根据完成键 ...

  10. redis集群的搭建

    1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...