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 \le t \le 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 \le n \le 2001≤n≤200, 1 \leq m \leq 2001≤m≤200).
The following nn lines contain mm integers each, the jj-th element of the ii-th line a_{ij}a
ij
is the number written in the jj-th cell of the ii-th row (0\leq a_{ij} \leq 10^6)(0≤a
ij
≤10
6
)
It is guaranteed that the sum of n\cdot mn⋅m over all test cases does not exceed 4\cdot10^44⋅10
4
.
Output
For each test case output a single integer, the maximum sum over all possible placements of the bishop.
Sample 1
Inputcopy Outputcopy
4
4 4
1 2 2 1
2 4 2 4
2 2 3 1
2 4 2 4
2 1
1
0
3 3
1 1 1
1 1 1
1 1 1
3 3
0 1 1
1 0 1
1 1 0
20
1
5
3
#include<iostream>
using namespace std;
int a[205][205],b[205][205];
int main(void) {
int n;
cin >> n;
int l, r;
for (int p = 0; p < n; p++) {
cin >> l >> r;
for (int i = 0; i < l; i++) {
for (int j = 0; j < r; j++) {
cin >> a[i][j];
b[i][j] = 0;
}
}
for (int i = 0; i < l; i++) {
for (int j = 0; j < r; j++) {
int i0 = i, j0 = j, i1 = i, j1 = j, i2 = i, j2 = j, i3 = i, j3 = j;
while (1) {
b[i][j] += a[i0][j0];
i0--;
j0--;
if (i0 == -1 || j0 == -1) {
break;
}
}
while (1) {
b[i][j] += a[i1][j1];
i1--;
j1++;
if (i1 == -1 || j1 == r) {
break;
}
}
while (1) {
b[i][j] += a[i2][j2];
i2++;
j2--;
if (i2 == l || j2 == -1) {
break;
}
}
while (1) {
b[i][j] += a[i3][j3];
i3++;
j3++;
if (i3 == l || j3 == r) {
break;
}
}
b[i][j] -= a[i][j] * 3;
}
}
int maxv = -1;
for (int i = 0; i < l; i++) {
for (int j = 0; j < r; j++) {
maxv = max(maxv, b[i][j]);
}
}
cout << maxv << endl;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现