zoukankan      html  css  js  c++  java
  • OpenJudge 2766 最大子矩阵

    1.链接:

    http://bailian.openjudge.cn/practice/2766

    2.题目:

    总Time Limit:
    1000ms
    Memory Limit:
    65536kB
    Description
    已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。

    比如,如下4 * 4的矩阵

    0 -2 -7 0
    9 2 -6 2
    -4 1 -4 1
    -1 8 0 -2

    的最大子矩阵是

    9 2
    -4 1
    -1 8

    这个子矩阵的大小是15。
    Input
    输入是一个N * N的矩阵。输入的第一行给出N (0 < N <= 100)。再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给出第二行的N个整数……)给出矩阵中的N2个整数,整数之间由空白字符分隔(空格或者空行)。已知矩阵中整数的范围都在[-127, 127]。
    Output
    输出最大子矩阵的大小。
    Sample Input
    4
    0 -2 -7 0 9 2 -6 2
    -4 1 -4  1 -1
    
    8  0 -2
    Sample Output
    15
    Source
    翻译自 Greater New York 2001 的试题

    3.思路:

    拓展的最大字段和。先遍历行的所有可能情况k=1-n。然后计算k行的矩阵每列的和,转为一维,在用最大字段和的方法求最大。

    4.代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     //freopen("C://input.txt","r",stdin);
    10 
    11     int i,j,k;
    12 
    13     int n;
    14     cin >> n;
    15 
    16     int **arr_matrix = new int*[n];
    17     for(i = 0; i < n; ++i) arr_matrix[i] = new int[n];
    18 
    19 
    20     for(i = 0;i < n; ++i)
    21     {
    22         for(j = 0;j < n; ++j)
    23         {
    24             cin >> arr_matrix[i][j];
    25         }
    26     }
    27 
    28     int *arr_temp = new int[n];
    29 
    30     int *dp = new int[n];
    31 
    32     int max_sum = arr_matrix[0][0];
    33     for(k = 0; k < n; ++k)
    34     {
    35         memset(arr_temp,0,sizeof(int) * n);
    36         for(j = 0; j < n; ++j)
    37         {
    38             for(i = 0; i < k; ++i) arr_temp[j] += arr_matrix[i][j];
    39         }
    40 
    41         for(i = k; i < n; ++i)
    42         {
    43             for(j = 0; j < n; ++j) arr_temp[j] += arr_matrix[i][j];
    44 
    45             memset(dp,0,sizeof(int) * n);
    46             dp[0] = arr_temp[0];
    47             for(j = 1; j < n; ++j)
    48             {
    49                 dp[j] = ((dp[j - 1] + arr_temp[j]) > arr_temp[j]) ? (dp[j - 1] + arr_temp[j]) : arr_temp[j];
    50                 if(max_sum < dp[j]) max_sum = dp[j];
    51             }
    52 
    53             for(j = 0; j < n; ++j) arr_temp[j] -= arr_matrix[i - k][j];
    54         }
    55     }
    56 
    57     cout << max_sum << endl;
    58 
    59     delete [] dp;
    60 
    61     delete [] arr_temp;
    62 
    63     for(i = 0; i < n; ++i) delete [] arr_matrix[i];
    64     delete [] arr_matrix;
    65 
    66     return 0;
    67 }
  • 相关阅读:
    【JDK】:java.lang.Integer源码解析
    使用truelicense实现用于JAVA工程license机制(包括license生成和验证)
    Android Button.getWidth()为0的问题
    nacos启动报错nacos Unable to start web server;
    Oracle字符集相关学习笔记记录
    Oracle触发器简单使用记录
    Oracle分析函数、窗口函数简单记录汇总
    Oracle远程数据建物化视图(materialized)创建简单记录,以及DBLINK的创建
    Oracle 行列转换函数pivot、unpivot的使用(二)
    Oracle递归查询(start with…connect by prior)
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3907533.html
Copyright © 2011-2022 走看看