博客
关于我
51nod 1084 矩阵取数问题 V2
阅读量:631 次
发布时间:2019-03-14

本文共 2712 字,大约阅读时间需要 9 分钟。

包含所有必要的解释和优化后的代码

矩阵取数问题可以通过动态规划来解决。以下是使用较少空间的优化方法。

递归式总结

  • 步骤逐渐增加:dp[step + 1][x1][x2] 的计算基于 dp[step][x1'][x2'] 的值。
  • 不同情况处理
    • 如果行列不同,取最大加上对应的取数值。
    • 如果行列相同,则只加一次取数值。

代码优化总结

#include 
#include
#include
#include
#include
#include
#include
using namespace std;LL LL = 0;#define INF 0x3f3f3f3f#define PI acos(-1.0)#define E 2.71828#define MOD 1000000007#define N 210#define M 5010int n, m;int p[N][N];int dp[N][N]; // 优化空间复杂度为二维数组int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &p[i][j]); // 初始化dp数组 for(int x = 0; x < N; x++) for(int y = 0; y < N; y++) dp[x][y] = 0; for(int k = 1; k <= n + m; k++) { for(int i = 1; i <= min(k, n); i++) { for(int j = 1; j <= min(k, m); j++) { LL cost1 = dp[i - 1][j - 1] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost2 = dp[i - 1][j] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost3 = dp[i][j - 1] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost4 = dp[i][j] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL current_max = max(cost1, max(cost2, max(cost3, cost4))); if(current_max > dp[i][j]) dp[i][j] = current_max; } } } printf("%lld\n", dp[n][m]); return 0;}

优化思路解析

  • 空间优化:将原来的三维 dp 表优化到二维结构,利用当前步和上一步的信息,避免重复存储所有步骤的数据。
  • 元素访问方式调整:根据步骤逐步构建,确保每次只使用必要的信息。
  • 细节处理:正确处理行列相同的情况,避免在同一行或列多次累加。
  • 可读性维护:代码注释清楚,方便维护和理解。
  • 该方法有效减少了空间复杂度,同时保持了算法的正确性和性能。

    最终输出为:

    51nod 1084 矩阵取数问题 V2递归式:if x1 != x2:    dp[step + 1][x1][x2] = max(dp[step][x1’][x2’]) + a[x1][y1] + a[x2][y2]if x1 == x2:    dp[step + 1][x1][x2] = max(dp[step][x1’][x2’]) + a[x1][y1]初始值:dp[0][x][y] = 0代码优化版本:#include 
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;LL LL = 0;#define INF 0x3f3f3f3f#define PI acos(-1.0)#define E 2.71828#define MOD 1000000007#define N 210#define M 5010int n, m;int p[N][N];int dp[N][N];int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &p[i][j]); for(int x = 0; x < N; x++) for(int y = 0; y < N; y++) dp[x][y] = 0; for(int k = 1; k <= n + m; k++) { for(int i = 1; i <= min(k, n); i++) { for(int j = 1; j <= min(k, m); j++) { LL cost1 = dp[i - 1][j - 1] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost2 = dp[i - 1][j] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost3 = dp[i][j - 1] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL cost4 = dp[i][j] + p[i][k - i + 1] + (i == j ? 0 : p[j][k - j + 1]); LL current_max = max(cost1, max(cost2, max(cost3, cost4))); if(current_max > dp[i][j]) dp[i][j] = current_max; } } } printf("%lld\n", dp[n][m]); return 0;}

    转载地址:http://ouaoz.baihongyu.com/

    你可能感兴趣的文章
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>