|NOIOJ|动态规划|1768:最大子矩阵

http://noi.openjudge.cn/ch0206/1768/

枚举矩阵起始行和结束行O(n^2), 然后再枚举一维O(n)求最大字段和即可

#include
#include  
#include   
#include 
#define ms(i,j) memset(i, j, sizeof(i)); 
using namespace std;
int a[105][105];
int n;
int main()  
{
	scanf("%d", &n);
	for (int i=1;i<=n;i++)
	for (int j=1;j<=n;j++)
	{
		scanf("%d", &a[i][j]);	
	} 
	int ans = -100000000;
	for (int i=1;i<=n;i++)//枚举矩阵起始行
	{
		int b[105];
		ms(b,0);
		for (int j=i;j<=n;j++)//枚举矩阵结束行
		{
			for (int k=1;k<=n;k++) b[k] += a[j][k];//累加和
			int f = -100000000;
			int ret = -100000000;
			for (int k=1;k<=n;k++) //动态规划求最大字段和
			{f = max(f+b[k], b[k]); ret = max(ret, f);}
			ans = max(ans, ret);
		}
	}
	printf("%d\n", ans);
    return 0;  
}  




你可能感兴趣的:(OpenJudge,Noi,动态规划)