Luogu1736 创意吃鱼法
DP。
设\(f(i,j)\)为右下角是\((i,j)\)的能吃到的最多的鱼,\(line(i,j)\)和\(col(i,j)\)分别为向左/右和向上能扩展到的全是0的最长的长度。
容易写出转移方程:
\(a[i][j]=0\)时\(line[i][j]=line[i][j-1]+1, col[i][j]=col[i-1][j]+1\)
\(a[i][j]=1\)时求一遍\(f[i][j]=min(f[i-1][j-1], min(line[i][j-1], col[i-1][j]))+1\)
DP两遍,分别是左上-右下和左下-右上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;
}
不如吃茶去