HDU 1081 to the max

将二维的动态规划问题转话为一维的动态规划问题

#include <iostream>
#include<string.h>
using namespace std;
int sequence[110][110];
int N;
int main()
{
    while(cin>>N)
    {
        int i,j,k;
        int data;
        int ans=-9999;
        for(i=1;i<=N;i++)
        {
            for(j=1;j<=N;j++)
            {
                cin>>data;
                sequence[i][j]=sequence[i][j-1]+data;//sequence[i][j]存储的是第i行J列之前的数据和(包括J列)
            }
        }
        for(i=1;i<=N;i++)
        {
            for(j=i;j<=N;j++)
            {
                int sum=0;
                for(k=1;k<=N;k++)
                {
                    if(sum<0){sum=0;}
                    sum+=sequence[k][j]-sequence[k][i-1];
                    if(sum>ans)
                    {
                        ans=sum;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(HDU)