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

    你可能感兴趣的文章
    ntelliJ IDEA 报错:找不到包或者找不到符号
    查看>>
    NTFS文件权限管理实战
    查看>>
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    ntp server 用法小结
    查看>>
    ntpdate 通过外网同步时间
    查看>>
    ntpdate同步配置文件调整详解
    查看>>
    NTPD使用/etc/ntp.conf配置时钟同步详解
    查看>>
    NTP及Chrony时间同步服务设置
    查看>>
    NTP服务器
    查看>>
    NTP配置
    查看>>
    NUC1077 Humble Numbers【数学计算+打表】
    查看>>
    NuGet Gallery 开源项目快速入门指南
    查看>>
    NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
    查看>>
    nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
    查看>>
    Nuget~管理自己的包包
    查看>>
    NuGet学习笔记001---了解使用NuGet给net快速获取引用
    查看>>
    nullnullHuge Pages
    查看>>
    NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
    查看>>
    null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
    查看>>