题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1224
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int a[N][N], s[N][N];
int get(int x1, int y1, int x2, int y2)
{
return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + a[i][j];
}
}
int res = -INF;
for (int x1 = 1; x1 <= n; x1++)
{
for (int y1 = 1; y1 <= n; y1++)
{
for (int x2 = x1; x2 <= n; x2++)
{
for (int y2 = y1; y2 <= n; y2++)
{
res = max(res, get(x1, y1, x2, y2));
}
}
}
}
printf("%d\n", res);
return 0;
}