51Nod 1084 矩阵取数问题 V2 双线程DP 滚动数组优化
第1行:2个数M N,中间用空格分隔,为矩阵的大小。(2 <= M, N <= 200)
第2 - N + 1行:每行M个数,中间用空格隔开,对应格子中奖励的价值。(1 <= A[i,j] <= 10000)
输出能够获得的最大价值。
3 3
1 3 3
2 1 3
2 2 1
17
思路:双线DP,看成两个人一起从(1,1)到(N,M),走的路径不能相同。
方法1:按照路径长度考虑,路径总长度:tot=x+y-1,dp[tot][x1][x2],两个人的横坐标x1,x2
#include <bits/stdc++.h>
using namespace std;
int ans[][],dp[][][];
int main() {
int M,N;
scanf("%d %d",&M,&N);
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
scanf("%d",&ans[i][j]);
memset(dp,,sizeof(dp));
for(int tot=;tot<=N+M-;++tot)//路径长度
for(int i=;i<=N&&(<=tot+-i);++i)
for(int j=;j<=N&&(<=tot+-j);++j) {
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i-][j-]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i-][j]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i][j-]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i][j])+ans[i][tot+-i]+ans[j][tot+-j];
if(i==j) dp[tot][i][j]-=ans[i][tot+-i];
}
printf("%d\n",dp[N+M-][N][N]);
return ;
}
方法2:按照走到走了几步,总的步数:tot=x+y-2
#include <bits/stdc++.h>
using namespace std;
int ans[][],dp[][][];
int main() {
int M,N;
scanf("%d %d",&M,&N);
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
scanf("%d",&ans[i][j]);
memset(dp,,sizeof(dp));
dp[][][]=ans[][];//一步都没走
for(int tot=;tot<=N+M-;++tot)//走了几步
for(int i=;i<=N&&(i-<=tot);++i)
for(int j=;j<=N&&(j-<=tot);++j) {
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i-][j-]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i-][j]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i][j-]);
dp[tot][i][j]=max(dp[tot][i][j],dp[tot-][i][j])+ans[i][tot+-i]+ans[j][tot+-j];
if(i==j) dp[tot][i][j]-=ans[i][tot+-i];
}
printf("%d\n",dp[N+M-][N][N]);
return ;
}
方法3:对方法2的优化,滚动数组
#include <stdio.h>
#include <string.h>
int ans[][],dp[][][];
int max(int a, int b) {if(a>=b) return a;return b;}
int main() {
int M,N;
scanf("%d %d",&M,&N);
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
scanf("%d",&ans[i][j]);
memset(dp,,sizeof(dp));
dp[][][]=ans[][];//一步都没走
int dir=;
//tot->走了几步
for(int tot=;tot<=N+M-;++tot) {
dir=-dir;
for(int i=;i<=N&&(i-<=tot);++i)
for(int j=;j<=N&&(j-<=tot);++j) {
dp[dir][i][j]=max(dp[dir][i][j],dp[-dir][i-][j-]);
dp[dir][i][j]=max(dp[dir][i][j],dp[-dir][i-][j]);
dp[dir][i][j]=max(dp[dir][i][j],dp[-dir][i][j-]);
dp[dir][i][j]=max(dp[dir][i][j],dp[-dir][i][j])+ans[i][tot+-i]+ans[j][tot+-j];
if(i==j) dp[dir][i][j]-=ans[i][tot+-i];
}
}
printf("%d\n",dp[dir][N][N]);
return ;
}
51Nod 1084 矩阵取数问题 V2 双线程DP 滚动数组优化的更多相关文章
- 51Nod 1084 矩阵取数问题 V2 —— 最小费用最大流 or 多线程DP
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1084 1084 矩阵取数问题 V2 基准时间限制:2 秒 空 ...
- 1084 矩阵取数问题 V2
1084 矩阵取数问题 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下 ...
- 51Nod 1084:矩阵取数问题 V2(多维DP)
1084 矩阵取数问题 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...
- 51nod1084 矩阵取数问题 V2
O(n4)->O(n3)妈呀为什么跑这么慢woc #include<cstdio> #include<cstring> #include<cctype> #i ...
- 51Nod 1083 矩阵取数问题(矩阵取数dp,基础题)
1083 矩阵取数问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下 ...
- [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
题目链接:http://acm.swust.edu.cn/problem/1084/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- 51nod 1411 矩阵取数问题 V3
给定一个m行n列的矩阵,你可以从任意位置开始取数,到达任意位置都可以结束,每次可以走到的数是当前这个数上下左右的邻居之一,唯一的限制是每个位置只能经过一次,也就是说你的路径不自交.所经过的数的总作为你 ...
- 51nod动态规划-----矩阵取数
一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值. 例如:3 * 3的方格. 1 3 3 2 1 3 2 2 1 能够获得的最 ...
- 51nod 1083 矩阵取数问题【动态规划】
一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值. 例如:3 * 3的方格. 1 3 3 2 1 3 2 2 1 能够获得的最 ...
随机推荐
- Linux下如何高效删除一个几十G的文本文件的最后一行或几行
当我们在服务器端记录日志或文本数据时,有时候会有需要删除一个大文件的最后几行,这时如何才能高效实现. 上网浏览终于找到dd命令,亲测如下,删除一个32GB的日志文件最后100行仅需要4分钟 [root ...
- bug:记最近出现的非功能bug
1.android 4.1.2 的兼容bug 一直以为Android 测试 4 5 6就可以了,结果发现Android4.1.2 和Android4.3之间还是有差距的. 处理办法:验证版本兼容的时候 ...
- 关于狄克斯特拉算法(dijkstra)总结
1,2,4是四个定点其他的是距离,从2到4最直接的就是2-4,但是不是最近的,需要舒展一下2-1-4,这样只有8.所以才是最短的.这个过程就是狄克斯特拉算法.下面进入正题: 我们这里定义图的编号为 ...
- Hadoop 中 最重要的两个模块
Hadoop 中 最重要的两个模块 HDFS 分布式的文件系统 主节点: NameNode SecondaryNamenode ResourceManager 从节点: DataNode Node ...
- 2017上海QCon之旅总结(下)
本来这个公众号的交流消息中间件相关的技术的.十月去上海参加了QCon,第一次参加这样的技术会议,感受挺多的,所以整理一下自己的一些想法接公众号和大家交流一下. 三天的内容还挺多的,之前已经有上篇和中篇 ...
- JAVANIO通道
package com.nio.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- html的块级元素和内联元素
常用的块级元素: address , center , div , dl ,, form , h1 , h2 , h3 , h4 , h5 , h6 , menu , ol , p , table , ...
- 【G彩娱乐网】作为一名程序员,我应该如何选购一台电脑?
G彩娱乐网说到程序员专用电脑,那肯定是苹果电脑.优点有很多,比如白平衡特别准.酷炫的黑科技.特别方便的软件等显而易见的优势:也有能够增加提案通过率.专注工作提高工作效率这样的玄学buff. 但是!并不 ...
- NHibernate Criteria中 Restriction与Expression的差别
http://stackoverflow.com/questions/5483393/nhibernate-criteria-restriction-vs-expression 据说是Restrict ...
- Winform C# 简单实现子窗口显示进度条
主窗口代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...