题目传送门
f[i][j][1/0]表示以(i,j)为右下角,当前格子是1/0的最大答案
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,ans,a[1501][1501],f[1501][1501][2]; inline int max(int s,int d) { if(s > d) return s; return d; } inline int min(int s,int d) { if(s < d) return s; return d; } 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]); for(int i = 1;i <= n; i++) for(int j = 1;j <= m; j++) { if(a[i][j]) f[i][j][1] = min(f[i-1][j][0],min(f[i][j-1][0],f[i-1][j-1][1])) + 1; else f[i][j][0] = min(f[i-1][j][1],min(f[i][j-1][1],f[i-1][j-1][0])) + 1; ans = max(ans,max(f[i][j][1],f[i][j][0])); } printf("%d",ans); return 0; }