57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#include <bits/stdc++.h>
|
||
/*
|
||
测试用例:
|
||
5 6
|
||
1 0 1 0 0 0
|
||
0 1 0 1 0 0
|
||
1 0 0 0 1 0
|
||
0 1 0 0 0 1
|
||
1 0 1 0 0 0
|
||
|
||
答案
|
||
4
|
||
|
||
|
||
测试用例II:
|
||
5 6
|
||
1 0 1 0 0 0
|
||
0 1 0 1 1 0
|
||
1 0 0 0 1 0
|
||
0 1 0 0 0 1
|
||
1 0 1 0 0 0
|
||
|
||
答案
|
||
2
|
||
*/
|
||
using namespace std;
|
||
const int N = 110;
|
||
int a[N][N];
|
||
int s[N][N];
|
||
int res;
|
||
|
||
int main() {
|
||
int n, m;
|
||
cin >> n >> m;
|
||
for (int i = 1; i <= n; i++)
|
||
for (int j = 1; j <= m; j++) {
|
||
cin >> a[i][j];
|
||
//维护二维前缀和
|
||
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
|
||
}
|
||
|
||
//直接在原数组上递推
|
||
for (int i = n - 1; i >= 1; i--) //从倒数第二行开始
|
||
for (int j = 1; j <= m; j++) //这个需要考虑一下边界问题
|
||
if (a[i][j]) { //只有当前位置大于0才有考虑的价值
|
||
a[i][j] += a[i + 1][j + 1]; //递推式,当前位置可以向下扩展的最长对角线长度
|
||
|
||
//利用二维前缀和计算区域的总和
|
||
int x1 = i, y1 = j, x2 = i + a[i][j] - 1, y2 = j + a[i][j] - 1;
|
||
int cnt = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
|
||
if (cnt == a[i][j])
|
||
res = max(res, a[i][j]); //在递推过程中不断求最大值
|
||
}
|
||
//输出
|
||
cout << res << endl;
|
||
return 0;
|
||
} |