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

    你可能感兴趣的文章
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>