前言:一开始写题的时候以为自己还记得,但是写的时候忘记了一个易错点
定义长度为 len , 那么 左上角的坐标为 ( i , j ) , 那么右下角的坐标为 为 ( i + len -1 ,j + len -1 )
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;int n;
const int N = (int)125;
int a[N][N];
int b[N][N];int main(){cin >> n;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin >> a[i][j];b[i][j] = b[i-1][j] + b[i][j-1] - b[i-1][j-1] + a[i][j];}} int ans = a[1][1];for (int len1 = 1; len1 <= n; len1++) {for (int len2 = 1; len2 <= n; len2++) {for (int i = 1; i <= n - len1 + 1; i++) {for (int j = 1; j <= n - len2 + 1; j++) {int x1 = i, y1 = j;int x2 = i + len1 - 1, y2 = j + len2 - 1;int t = b[x2][y2] - b[x1-1][y2] - b[x2][y1-1] + b[x1-1][y1-1];ans = max(ans, t);}}}}cout << ans ;return 0;
}