hdu 1081 To The Max
2011-10-01 17:40
239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1081
这道题目是一道比较经典的动态规划问题,做了这道题目才知道原来一切都是万变不离其宗是啥子意思!这是一道求二维矩阵中最大子矩阵和的问题,和一维求最大字段和有一
定的联系!以前看过这道题,但是没有想出来怎么做,今天刷题刷到这里,又看了看别人的解析,才马马虎虎做出来!
求解过程中,固定行,然后将任意两行之间的所有列值加起来,然后求这个序列中最大子序列和,就是所求的最大子矩阵和
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=105;
int n;
int dp[maxn][maxn];
int b[maxn];
//一维的求最大字段和问题
int maxsub(int b[],int n)
{
int sum=0,max=0;
for(int i=0;i<n;i++)
{
if(sum>0) sum+=b[i];
else sum=b[i];
if(sum>max) max=sum;
}
return max;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int max=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&dp[i][j]);
//从一维的基础上延伸出来
//将两行之间的所有列值加起来,然后求这个序列中最大字段和
for(int i=0;i<n;i++)
{
memset(b,0,sizeof(b));
for(int j=i;j<n;j++)
{
for(int k=0;k<n;k++)
b[k]+=dp[j][k];
int ans=maxsub(b,n);
if(ans>max) max=ans;
}
}
printf("%d\n",max);
}
return 0;
}
这道题目是一道比较经典的动态规划问题,做了这道题目才知道原来一切都是万变不离其宗是啥子意思!这是一道求二维矩阵中最大子矩阵和的问题,和一维求最大字段和有一
定的联系!以前看过这道题,但是没有想出来怎么做,今天刷题刷到这里,又看了看别人的解析,才马马虎虎做出来!
求解过程中,固定行,然后将任意两行之间的所有列值加起来,然后求这个序列中最大子序列和,就是所求的最大子矩阵和
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=105;
int n;
int dp[maxn][maxn];
int b[maxn];
//一维的求最大字段和问题
int maxsub(int b[],int n)
{
int sum=0,max=0;
for(int i=0;i<n;i++)
{
if(sum>0) sum+=b[i];
else sum=b[i];
if(sum>max) max=sum;
}
return max;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int max=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&dp[i][j]);
//从一维的基础上延伸出来
//将两行之间的所有列值加起来,然后求这个序列中最大字段和
for(int i=0;i<n;i++)
{
memset(b,0,sizeof(b));
for(int j=i;j<n;j++)
{
for(int k=0;k<n;k++)
b[k]+=dp[j][k];
int ans=maxsub(b,n);
if(ans>max) max=ans;
}
}
printf("%d\n",max);
}
return 0;
}
相关文章推荐
- To The Max_hdu_1081(经典DP).java
- hdu 1081 To The Max
- HDU1081 To The Max
- HDU 1081 to the max
- POJ 1050 && HDU 1081 To the Max(前缀和)
- hdu 1081 to the max
- hdu 1081 To The Max
- HDU 1081 To The Max 最大子矩阵和 .
- HDU1081 To The Max
- hdu 1081 To The Max(最大子矩阵和)
- HDU 1081 To The Max(最大子矩阵)
- hdu 1081 To The Max
- hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
- HDU 1081 To The Max
- hdu 1081 to the max
- hdu 1081 To The Max(最小子阵和)
- HDU 1081 To The Max
- hdu 1081 To the Max
- hdu 1081 To The Max最大矩阵和(动态规划)
- HDU 1081 To The Max