#include<iostream>
#include<fstream>
using namespace std;
//#define DEBUG
/* 276K 63MS */
/* 在提交的代码中有0MS的,以后可以考虑优化或是找寻新的结题思路 */
static int matrix[100][100];
static int vct[100];
int maxsubseq(int a[], int n)
{
int max = 0;
int maxsubi = 0;
int i;
#ifdef PRINT
int submax[N];
int e = 0, b = 0;
#endif
for (i = 0; i < n; i++)
{
int tmp = maxsubi + a[i];
maxsubi = tmp > 0 ? tmp : 0;
#ifdef PRINT
submax[i] = maxsubi;
#endif
if (maxsubi > max)
{
#ifdef PRINT
e = i;
#endif
max = maxsubi;
}
}
#ifdef PRINT
for(i = e; i >= 0; i--)
{
if (submax[i] == 0)
{ b = i + 1; break; }
}
printf("%d %d\n", b, e);
for (i = b; i <= e; i++)
printf("%d ", a[i]);
printf("\n");
#endif
return max;
}
int main()
{
#ifdef DEBUG
fstream cin("G:\\book\\algorithms\\acm\\Debug\\dat.txt");
#endif
int n;
while (cin >> n)
{
int i, j, c, r;
int max =0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
for (i = 0; i < n; i++) /* 列举出所有的起始行,结束行的情况 */
{
for (j = i; j < n; j++)
{
memset(vct, 0, sizeof vct);
for (r = i; r <= j; r++)
for (c = 0; c < n; c++)
vct[c] += matrix[r][c];
int tmp = maxsubseq(vct, n);
if (tmp > max) max = tmp;
}
}
printf("%d\n", max);
}
return 0;
}
计算过程需要依赖一维问题的求解。
POJ 1050 二维最大连续子向量问题
最新推荐文章于 2022-09-02 19:05:00 发布