HDU 1081 To The Max

题意:输入n,然后输入n行n列的矩阵,找出这个矩阵中的子矩阵最大值。

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <algorithm>

using namespace std;

const int maxn = 105;
const int oo = 222;

int a[maxn][maxn];

int main()
{
    int n;
    int x;
    while(~scanf("%d", &n))
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d", &x);
                a[i][j] = a[i][j-1]+x;
            }
        }
        int Max = a[1][1];
        int sum;
        for(int i=1; i<=n; i++)//枚举列的数目
        {
            for(int j=i; j<=n; j++)//从第j列开始
            {
                sum = 0;
                for(int k=1; k<=n; k++)//枚举连续行
                {
                    sum+=a[k][j]-a[k][j-i];//找出连续行连续列的最大值,即子矩阵的最大值
                    if(sum<0)
                        sum = 0;
                    if(sum>Max)
                        Max = sum;
                }
            }
        }

        printf("%d
", Max);

    }

    return 0;
}
原文地址:https://www.cnblogs.com/mengzhong/p/5443427.html