• POJ 1050 To the Max


    To the Max
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 36627   Accepted: 19262

    Description

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. 
    As an example, the maximal sub-rectangle of the array: 

    0 -2 -7 0 
    9 2 -6 2 
    -4 1 -4 1 
    -1 8 0 -2 
    is in the lower left corner: 

    9 2 
    -4 1 
    -1 8 
    and has a sum of 15. 

    Input

    The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

    Output

    Output the sum of the maximal sub-rectangle.

    Sample Input

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

    Sample Output

    15
    题目大意:最大子矩阵和问题。
    解题方法:动态规划。
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    int maze[105][105];
    int Subsum[105];
    
    int GetSubsum(int n)
    {
        int sum = 0;
        int Maxsum = -10000000;
        for (int i = 0; i < n; i++)
        {
            if (sum >= 0)
            {
                sum += Subsum[i];
            }
            else
            {
                sum = Subsum[i];
            }
            Maxsum = max(Maxsum, sum);
        }
        return Maxsum;
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        int Maxsum = -10000000;
        for (int i = 0; i< n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                scanf("%d", &maze[i][j]);
            }
        }
        for (int i = 0; i < n; i++)
        {
            memset(Subsum, 0, sizeof(Subsum));
            for (int j = i; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    Subsum[k] += maze[k][j];
                }
                Maxsum = max(Maxsum, GetSubsum(n));
            }
        }
        printf("%d
    ", Maxsum);
        return 0;
    }
  • 相关阅读:
    美国商务签证面试经历
    正则表达式匹配字符串中的数值部分并将其返回
    在WPF中使用水晶报表for vs2010的方法
    Wpf中用代码执行控件的单击事件
    WPF项目中使用水晶报表for vs2010时的一个找不到程序集的问题
    WinForm实现全屏方法
    wpf中将数据导出为Excel
    WinForm实现窗体上控件的自由拖动
    在WPF中使用WinForm控件方法
    多样、互动的WinForm UI设计与开发思路(Flash、Html等)
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3205052.html
Copyright © 2020-2023  润新知