luogu P2701 [USACO5.3]巨大的牛棚Big Barn |动态规划

题目描述

农夫约翰想要在他的正方形农场上建造一座正方形大牛棚。他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方。我们假定,他的农场划分成 N x N 的方格。输入数据中包括有树的方格的列表。你的任务是计算并输出,在他的农场中,不需要砍树却能够修建的最大正方形牛棚。牛棚的边必须和水平轴或者垂直轴平行。

EXAMPLE

考虑下面的方格,它表示农夫约翰的农场,‘.'表示没有树的方格,‘#'表示有树的方格

1 2 3 4 5 6 7 8

1 . . . . . . . .

2 . # . . . # . .

3 . . . . . . . .

4 . . . . . . . .

5 . . . . . . . .

6 . . # . . . . .

7 . . . . . . . .

8 . . . . . . . .

最大的牛棚是 5 x 5 的,可以建造在方格右下角的两个位置其中一个。

输入格式

Line 1: 两个整数: N (1 <= N <= 1000),农场的大小,和 T (1 <= T <= 10,000)有树的方格的数量

Lines 2..T+1: 两个整数(1 <= 整数 <= N), 有树格子的横纵坐标

输出格式

只由一行组成,约翰的牛棚的最大边长。


#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e3+5;
int a[N][N],f[N][N];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		a[x][y]=1;
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++){
		if(!a[i][j])f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;
	    ans=max(ans,f[i][j]);
	    
	}
    cout<<ans<<endl;
}
原文地址:https://www.cnblogs.com/naruto-mzx/p/11854702.html