X-Sum - CodeForces 1676D - Virtual Judge (csgrandeur.cn)
走四个方向的sum:
LL check(int x,int y)
{
LL sum1=0;
for(int i=x+1,j=y+1;i<=n&&j<=m;i++,j++) sum1+=a[i][j];
for(int i=x,j=y;i>0&&j>0;i--,j--) sum1+=a[i][j];
for(int i=x-1,j=y+1;i>0&&j<=m;i--,j++) sum1+=a[i][j];
for(int i=x+1,j=y-1;i<=n&&j>0;i++,j--) sum1+=a[i][j];
return sum1;
}
code:
#include<bits/stdc++.h>
#define x first
#define y second
#define mak make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(a) cout<<a<<'\n'
#define endl '\n'
#define umap unordered_map
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1e5+10,M=1,inf=0x3f3f3f3f,mod=1e9+7;
int t,n,m,a[250][250];
LL check(int x,int y)
{
LL sum1=0;
for(int i=x+1,j=y+1;i<=n&&j<=m;i++,j++) sum1+=a[i][j];
for(int i=x,j=y;i>0&&j>0;i--,j--) sum1+=a[i][j];
for(int i=x-1,j=y+1;i>0&&j<=m;i--,j++) sum1+=a[i][j];
for(int i=x+1,j=y-1;i<=n&&j>0;i++,j--) sum1+=a[i][j];
return sum1;
}
void solve()
{
cin>>n>>m;
LL sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
sum=max(sum,check(i,j));
}
}
cout<<sum<<endl;
}
int main()
{
IOS;
cin>>t;
while(t--)
{
solve();
}
return 0;
}