USACO Section 5.3 Big Barn(dp)

本文介绍了一个USACO竞赛中的经典问题——如何在一块被树木占据的农场中找到最大的空地来建造一个正方形的大谷仓。通过动态规划的方法解决了这一问题,并给出了完整的C++代码实现。

 USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) 。dp(i,j)表示以坐标(i,j)为左上角的barn边长最大值,dp(i+1,j),dp(i,j+1)分别表示向右和向下能扩展的最大边长,但是以此为正方形时,右下方的一个格子没有考虑到,所以就+个dp(i+1,j+1)。边界为:dp(i,j)=1(i==n-1或j==n-1)。

-------------------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<r;i++)
using namespace std;
const int maxn=1000+5;
int d[maxn][maxn];
int ok[maxn][maxn];
int n;
int dp(int i,int j) {
int &ans=d[i][j];
if(ans>=0) return ans;
ans=0;
if(!ok[i][j]) return ans;
if(i==n-1 || j==n-1) return ans=1;
return ans=min(dp(i+1,j+1),min(dp(i+1,j),dp(i,j+1)))+1;
}
int main()
{
freopen("bigbrn.in","r",stdin);
freopen("bigbrn.out","w",stdout);
clr(ok,-1);
int t;
cin>>n>>t;
rep(i,t) {
int a,b;
scanf("%d%d",&a,&b);
ok[--a][--b]=0;
}
clr(d,-1);
int ans=0;
rep(i,n)
   rep(j,n) ans=max(ans,dp(i,j));
   
cout<<ans<<endl;
return 0;
}

 

------------------------------------------------------------------------------- 

Big Barn
A Special Treat

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.

EXAMPLE

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.

PROGRAM NAME: bigbrn

INPUT FORMAT

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

SAMPLE INPUT (file bigbrn.in)

8 3 2 2 2 6 6 3 

OUTPUT FORMAT

The output file should consist of exactly one line, the maximum side length of John's barn.

SAMPLE OUTPUT (file bigbrn.out)

5

 

转载于:https://www.cnblogs.com/JSZX11556/p/4342967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值