zoj 1074 To the Max 最大子矩阵之和

//这题开始方法想错了,调了半天,才发现动规方程列不正确

//这题可以看做一维数组的连续子串最大和的扩展

//方法是把二维转换成一维,再用一维的动规方程求

//把多行合并成一行求

#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;



const int N = 105;

const int INF = 1000000000;



int num[N][N];

int sum[N];

int dp[N];



int n, ans;



void maxsubsum()

{

	dp[0] = sum[0];

	for (int i = 1; i < n; i++)

	{

		dp[i] = max(dp[i-1]+sum[i], sum[i]);

		ans = max(ans, dp[i]);

	}

}



int main()

{

	while (scanf("%d", &n) != EOF)

	{

		ans = -INF;

		for (int i = 0; i < n; i++)

			for (int j = 0; j < n; j++)

				scanf("%d", &num[i][j]);



		for (int i = 0; i < n ; i++)

		{

			for (int j = i; j < n; j++)

			{

				memset(sum, 0, sizeof(sum));

				//把第i行到第j行压缩成一行

				for (int s = 0; s < n; s++)

				for (int k = i; k <= j; k++)

					sum[s] += num[k][s];



				//求一维数组连续子串和的最大值

				maxsubsum();

			}

		}

		printf("%d\n", ans);

	}

	return 0;

}

你可能感兴趣的:(max)