Codeforces Round #790 (Div. 4)D. X-Sum

Timur's grandfather gifted him a chessboard to practice his chess skills. This chessboard is a grid aa with nn rows and mm columns with each cell having a non-negative integer written on it.

Timur's challenge is to place a bishop on the board such that the sum of all cells attacked by the bishop is maximal. The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. Help him find the maximal sum he can get.

Input

The first line of the input contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of test cases follows.

The first line of each test case contains the integers nn and mm (1≤n≤2001≤n≤200, 1≤m≤2001≤m≤200).

The following nn lines contain mm integers each, the jj-th element of the ii-th line aijaij is the number written in the jj-th cell of the ii-th row (0≤aij≤106)(0≤aij≤106)

It is guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 4⋅1044⋅104.

Output

For each test case output a single integer, the maximum sum over all possible placements of the bishop.

 


#include <iostream>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
typedef long long LL;
 
const int N=210;
 
int n,m;
int f[N];
int t;
int g[N][N];
 
int main()
{
	int t ;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				cin>>g[i][j];
			}
			int sum=0;
		int dx[]={-1,-1,1,1};
		int dy[]={-1,1,-1,1};
		for(int i=1;i<=n;i++)	
			for(int j=1;j<=m;j++)
			{
				int ans=0;
				ans+=g[i][j];
				for(int l=1;l<=max(n,m);l++)
				{
					for(int k=0;k<4;k++)
					{
						int a=l*dx[k]+i;
						int b=l*dy[k]+j;
						if(a>0&&a<=n&&b>0&&b<=m)
						ans+=g[a][b];
					}
				}
				sum=max(sum,ans);	
			}
		cout<<sum<<endl;
	}
	return 0;
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值