首页 技术 正文
技术 2022年11月20日
0 收藏 450 点赞 2,254 浏览 2367 个字

基准时间限制:2 秒 空间限制:131072 KB 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上。第1遍时只能向下和向右走,第2遍时只能向上和向左走。两次如果经过同一个格子,则该格子的奖励只计算一次,求能够获得的最大价值。

 例如:3 * 3的方格。 1 3 32 1 32 2 1 能够获得的最大价值为:17。1 -> 3 -> 3 -> 3 -> 1 -> 2 -> 2 -> 2 -> 1。其中起点和终点的奖励只计算1次。 Input

第1行:2个数M N,中间用空格分隔,为矩阵的大小。(2 <= M, N <= 200)
第2 - N + 1行:每行M个数,中间用空格隔开,对应格子中奖励的价值。(1 <= A[i,j] <= 10000)

Output

输出能够获得的最大价值。

Input示例

3 3
1 3 3
2 1 3
2 2 1

Output示例

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860