poj 1050 To the Max (简单dp)

题目链接:http://poj.org/problem?id=1050

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 105;
const int INF = 0x3f3f3f;

int dp[maxn];
int sum[maxn][maxn];
int ans;

int main()
{
    //freopen("E:\acm\input.txt","r",stdin);
    int N;
    cin>>N;
    for(int i=0;i<=N;i++)  sum[0][i] = sum[i][0] = 0;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++){
            int a;
            scanf("%d",&a);
            sum[i][j] = sum[i-1][j] + a;
    }
    ans = -INF;
    for(int i=1;i<=N;i++)
        for(int j=i;j<=N;j++){
            memset(dp,0,sizeof(dp));
            for(int k=1;k<=N;k++){
                dp[k] = max(0,dp[k-1]) + sum[j][k] - sum[i][k];
                ans = max(dp[k],ans);
            }
    }
    printf("%d
",ans);
}
//自己的做时候,一直想怎样写dp并表示出状态转移方程,但是就是怎么也想不出来,觉得不好表示。没办法看了别人的想法,马上反应过来。头脑僵化了啊!!!
View Code
原文地址:https://www.cnblogs.com/acmdeweilai/p/3286190.html