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.

 

Codeforces Round #790 (Div. 4)D. X-Sum_第1张图片


#include 
#include 
#include 
 
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<

 

你可能感兴趣的:(codeforces,模拟,蓝桥杯,算法,c++)