【矩阵压缩】 poj 1050

题意:给一个矩阵,里面有正负数,求子矩阵和的最大值

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <memory.h>
using namespace std;
int s[105][105],dp[105],n,temp[105];
int main()
{
    // freopen("in.txt","r",stdin);
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>s[i][j];
    int maxx = 0;
    for (int i=1;i<=n;i++) //start from line i
    {
        memset(temp,0,sizeof(temp));
        for (int j=i;j<=n;j++) //end at line j
        {
            for (int k=1;k<=n;k++)
            {
                temp[k]+=s[j][k];
                if (temp[k]+dp[k-1]>0)
                dp[k]=temp[k]+dp[k-1];
                else
                dp[k]=0;
                if (maxx<dp[k])
                    maxx =dp[k];
            }
        }
     }
    cout << maxx << endl;
}
原文地址:https://www.cnblogs.com/balfish/p/4014582.html