输入数据 循环时注意外层和内层的顺序(y x)
处理结果时r要从l开始
#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN=2e2;
int a[MAXN][MAXN];//前缀和
int n;
int main(){
scanf("%d",&n);
int ans=-2e9;
bool isBigger=false;
for(int y=1;y<=n;y++){
for(int x=1;x<=n;x++){
scanf("%d",&a[x][y]);
ans=max(ans,a[x][y]);
if(a[x][y]>0)isBigger=1;
if(x!=1){
a[x][y]+=a[x-1][y];//前缀和
}
}
}
if(!isBigger){
cout<<ans<<endl;
return 0;
}
int nowAns=0;
for(int l=1;l<=n;l++){
for(int r=l;r<=n;r++){
nowAns=0;
for(int line=1;line<=n;line++){
int tmp=a[r][line]-a[l-1][line];
nowAns+=tmp;
ans=max(ans,nowAns);
if(nowAns<0)nowAns=0;
}
}
}
cout<<ans<<endl;
return 0;
}