题意:

两个人一个从左上角一个从左下角分别开始走分别走向右下角和右上角,(矩阵每个格子有数)问到达终点后可以得到的最大数是多少,并且条件是他们两个相遇的时候那个点的数不能算

思路:

首先这道题如果暴力搜索一般是gg了,所以考虑动态规划

我们设起点为st(1,1),终点为ed(n,m),相遇的点为now(i,j)

问题转化为计算st→now + now→ed的值(不包含now)

这个问题可以分解为求st→nownow→ed的值,st→now = dp[st][now],那么now→ed怎么求呢

可以反过来思考,now→ed其实就是ed→now的值,反向dp即可

还有个很重要的问题

在相遇的时候,即在点now时,每条路径只能走相对的边,如图



如果走的是临边,效果可能不一样,可以试试。。。

代码:

#include<iostream>
#include<cstring>
#define max(a, b) ((a)>(b)?(a):(b))
using namespace std;
typedef long long ll;
const int maxn = 1010;
int mp[maxn][maxn];
int dp1[maxn][maxn],dp2[maxn][maxn],dp3[maxn][maxn],dp4[maxn][maxn];
int main() {
memset(dp1, 0, sizeof dp1);
memset(dp2, 0, sizeof dp2);
memset(dp3, 0, sizeof dp3);
memset(dp4, 0, sizeof dp4);
int n,m;
scanf("%d %d", &n,&m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
scanf("%d", &mp[i][j]);
}
}
//从右下 走
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + mp[i][j];//到点(i,j)有两种方法,以下如此
}
}
//从左上 走
for(int i = n; i >= 1; i--) {
for(int j = m; j >= 1; j--) {
dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + mp[i][j];
}
}
//从右上 走
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= m; j++) {
dp3[i][j] = max(dp3[i+1][j], dp3[i][j-1]) + mp[i][j];
}
}
//从左下 走
for(int i = 1; i <= n; i++) {
for(int j = m; j >= 1; j--) {
dp4[i][j] = max(dp4[i-1][j], dp4[i][j+1]) + mp[i][j];
}
} ll ans = -1;
for(int i = 2; i < n; i++) {
for(int j = 2; j < m; j++) {
ans = max(dp1[i-1][j] + dp2[i+1][j] + dp3[i][j-1] + dp4[i][j+1], ans);
ans = max(dp1[i][j-1] + dp2[i][j+1] + dp3[i+1][j] + dp4[i-1][j], ans);
//把横向穿过和纵向穿过,两者进行枚举
}
}
printf("%d\n", ans);
return 0;
}

CF 429B B.Working out (四角dp)的更多相关文章

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

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

  2. B. Working out 四角dp

    https://codeforces.com/problemset/problem/429/B 这个题目之前写过,不过好像..忘记了,今天又没有写出来,应该之前没有想明白... 这个应该算一个四角dp ...

  3. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  4. CF 372B Counting Rectangles is Fun [dp+数据维护]

    题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 watermark/2/text/aHR0cDovL2Jsb2 ...

  5. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

  6. CF 407B Long Path[观察性质 DP]

    B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. cf.301.D. Bad Luck Island(dp + probabilities)

    D. Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  9. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

随机推荐

  1. 【转】React 常用面试题目与分析

    作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...

  2. Lucas模板&快速幂模板

    /* *********************************************** Author :guanjun Created Time :2016/5/20 0:28:36 F ...

  3. go语言---slice

    go语言---slice https://blog.csdn.net/cyk2396/article/details/78893420 一.数组切片的使用: //1.基于数组创建数组切片 var ar ...

  4. libnids 中哈希表的建立

    //hash.c #include <sys/types.h>#include <sys/time.h>#include <stdio.h>#include < ...

  5. sql server使用维护计划定时备份完整数据库、差异数据库

    我配置的是: 一个月执行一次完整备份数据库,删除三个月前备份文件.每天执行一次差异备份,删除一个月钱备份文件. 1.管理-维护计划   右键-新建维护计划 2.创建子计划 3.分别配置作业计划属性(执 ...

  6. robotframework - Edit编辑器

    1.测试项目&套件 提供的Edit编辑器 2.在 Edit 标签页中主要分:加载外部文件.定义内部变量.定义元数据等三个部分. (1):加载外部文件Add Library:加载测试库,主要是[ ...

  7. ACM_Fibonacci数(同余)

    Fibonacci数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 斐波那契数列定义如下:f(0)=0,f(1)=1,f(n+2 ...

  8. 清除WebSphere部署应用所对应的JSP缓存

    Web应用部署在WebSphere Application Server v8.5后程序一般放置在<AppServer>/profiles/<profile_name>/ins ...

  9. Android 性能优化(25)*性能工具之「Systrace」Analyzing UI Performance with Systrace:用Systrace得到ui性能报告

    Analyzing UI Performance with Systrace In this document Overview 简介 Generating a Trace  生成Systrace文件 ...

  10. 转 awr自动收集脚本

    1. remote get awr report #!/usr/bin/ksh ####sample: sh awr.sh 20170515 20170516 AWR ### default it w ...