parent
8616033a39
commit
bf1cf065cc
@ -0,0 +1,32 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 130;
|
||||
int n;
|
||||
int a[N][N]; // 存储题目中的矩阵
|
||||
int s[N][N]; // 二维前缀和
|
||||
int qz[N][N]; // qz[i][j]指的是第i行到j的前缀和
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
cin >> a[i][j];
|
||||
qz[i][j] = qz[i][j - 1] + a[i][j]; // 求前缀和
|
||||
s[i][j] = qz[i][j] + s[i - 1][j]; // 计算s
|
||||
}
|
||||
}
|
||||
|
||||
int mx = INT_MIN; // 存储答案
|
||||
// 遍历左上角坐标,与,右下角坐标
|
||||
for (int x1 = 1; x1 <= n; x1++) {
|
||||
for (int y1 = 1; y1 <= n; y1++) {
|
||||
for (int x2 = 1; x2 <= n; x2++)
|
||||
for (int y2 = 1; y2 <= n; y2++) {
|
||||
if (x2 < x1 || y2 < y1) continue; // 如果左上角比右下角还要大,就不用求了,下一个
|
||||
mx = max(mx, s[x2][y2] + s[x1 - 1][y1 - 1] - s[x2][y1 - 1] - s[x1 - 1][y2]); // 求最大值
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << mx << endl; // 输出
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue