洛谷 P1736 创意吃鱼法

和最大正方形很像。
记得对角线有两条
还有2500不要开成3000,否则会mle

#include<cstdio>
#include<algorithm>
#include<cstring>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;

const int MAXN = 2512;
int f[MAXN][MAXN], x[MAXN][MAXN], y[MAXN][MAXN];
int a[MAXN][MAXN], n, m, ans, now;

int main()
{
	scanf("%d%d", &n, &m);
	_for(i, 1, n)
		_for(j, 1, m)
			scanf("%d", &a[i][j]);
	 
	_for(i, 1, n)
		_for(j, 1, m)	
		{
			if(a[i][j])
			{	
				f[i][j] = min(f[i-1][j-1], min(y[i][j-1], x[i-1][j])) + 1;
				ans = max(ans, f[i][j]);
			}
			else 
			{
				x[i][j] = x[i-1][j] + 1;
				y[i][j] = y[i][j-1] + 1;
			}
	}
	
	memset(f, 0, sizeof(f));
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	
	_for(i, 1, n)
		for(int j = m; j >= 1; j--)	
		{
			if(a[i][j])
			{	
				f[i][j] = min(f[i-1][j+1], min(y[i][j+1], x[i-1][j])) + 1;
				ans = max(ans, f[i][j]);
			}
			else 
			{
				x[i][j] = x[i-1][j] + 1;
				y[i][j] = y[i][j+1] + 1;
			}
	}

	printf("%d\n", ans);
	
	return 0;
}

 

posted @ 2018-09-08 13:59  Sugewud  阅读(98)  评论(0编辑  收藏  举报