博客
关于我
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/

    你可能感兴趣的文章
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>