Farmer John wants to place a big square barn on his square farm. He hates to cut down trees on his farm and wants to find a location for his barn that enables him to build it only on land that is already clear of trees. For our purposes, his land is divided into N x N parcels. The input contains a list of parcels that contain trees. Your job is to determine and report the largest possible square barn that can be placed on his land without having to clear away trees. The barn sides must be parallel to the horizontal or vertical axis.
Consider the following grid of Farmer John's land where `.' represents a parcel with no trees and `#' represents a parcel with trees:
1 2 3 4 5 6 7 8 1 . . . . . . . . 2 . # . . . # . . 3 . . . . . . . . 4 . . . . . . . . 5 . . . . . . . . 6 . . # . . . . . 7 . . . . . . . . 8 . . . . . . . .
The largest barn is 5 x 5 and can be placed in either of two locations in the lower right part of the grid.
Line 1: | Two integers: N (1 <= N <= 1000), the number of parcels on a side, and T (1 <= T <= 10,000) the number of parcels with trees |
Lines 2..T+1: | Two integers (1 <= each integer <= N), the row and column of a tree parcel |
8 3 2 2 2 6 6 3
The output file should consist of exactly one line, the maximum side length of John's barn.
5
题意:给你一个n*n的矩阵,找到一个子方阵,满足方阵中没有树,求最大的边长
分析:很简单的一道DP题,对比(最大子矩阵来说),很容易想到转移方程f[i][j]=min(f[i-1][j],f[i][j-1])+1当然还得判断对角上的点是否是树。。。
代码:
/* ID: 15114582 PROG: bigbrn LANG: C++ */ #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int mm=1111; int f[mm][mm]; bool g[mm][mm]; int i,j,n,t,ans; int main() { freopen("bigbrn.in","r",stdin); freopen("bigbrn.out","w",stdout); while(~scanf("%d%d",&n,&t)) { memset(g,1,sizeof(g)); for(i=0;i<=n;++i) for(j=0;j<=n;++j) f[i][j]=g[i][j]=0; while(t--) { scanf("%d%d",&i,&j); g[i][j]=1; } ans=0; for(i=1;i<=n;++i) for(j=1;j<=n;++j) if(!g[i][j]) { t=min(f[i-1][j],f[i][j-1]); f[i][j]=t+(!g[i-t][j-t]); if(!f[i][j])f[i][j]=1; ans=max(ans,f[i][j]); } printf("%d\n",ans); } return 0; }