DP.
Set \ (f (I, j) \) to \ (I, j) \) in the lower right corner, \ (line (I, j) \) and \ (COL (I, j) \) are the longest lengths that can be expanded to left/right and up to 0, respectively.
Easy to write the transfer equation:
\ (A [I] [J] = 0 \) \ (line [I] [J] = line [I] [J-1] + 1, col [I] [J] = Col [I-1] [J] + 1 \)
\ (A [I] [J] = 1 \) returns \ (F [I] [J] = min (F [I-1] [J-1], min (line [I] [J-1], Col [I-1] [J]) + 1 \)
DP twice, which is upper left-lower right and lower left-upper right qwq
Code:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int a[2501][2501], line[2501][2501], col[2501][2501], f[2501][2501];int n, m, ans;int main(){ scanf("%d%d", &n, &m); for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ scanf("%d", &a[i][j]); if(!a[i][j]){ line[i][j]=line[i][j-1]+1; col[i][j]=col[i-1][j]+1; }else { f[i][j]=min(f[i-1][j-1], min(line[i][j-1], col[i-1][j]))+1; } ans=max(ans, f[i][j]); } } memset(f, 0, sizeof f); memset(line, 0, sizeof line); memset(col, 0, sizeof col); for(int i=1; i<=n; i++) for(int j=m; j>0; j--){ if(!a[i][j]){ line[i][j]=line[i][j+1]+1; col[i][j]=col[i-1][j]+1; }else { f[i][j]=min(f[i-1][j+1], min(line[i][j+1], col[i-1][j]))+1; } ans=max(ans, f[i][j]); } printf("%d", ans); return 0;}
Luogu1736 creative fish farming