USACO 5.3 Big Barn

用DP写的,题目限定了为最大正方形,所以用dp写起来非常顺手~~

dp[i][j]表示以点(i,j)为右下角的正方形的最大边长。

dp[i][j] = max(dp[i][j], min(dp[i-1][j-1]+1,sx, sy[j]) )~~开代码吧,很鲜明~

dp[i][j] == -1表示不存在合法正方形

/*
ID: zlqest11
LANG: C++
TASK: bigbrn
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1002;
int n, m, dp[N][N];
int sx, sy[N];

int main()
{
int ans = 0, x, y;
freopen("bigbrn.in", "r", stdin);
freopen("bigbrn.out", "w", stdout);
scanf("%d%d", &n, &m);
memset(dp, 0, sizeof(dp));
for(int i = 0; i < m; i++){
scanf("%d%d", &x, &y);
dp[x][y] = -1;
}
memset(sy, 0, sizeof(sy));
for(int i = 1; i <= n; i++){
sx = 0;
for(int j = 1; j <= n; j++){
if(dp[i][j] == -1){
sx = sy[j] = 0;
continue;
}
else{
sx++, sy[j]++;
dp[i][j] = 1;
dp[i][j] = max(dp[i][j], min(dp[i-1][j-1]+1, min(sx, sy[j])));
ans = max(ans, dp[i][j]);
}
}
}
printf("%d\n", ans);
return 0;
}



posted on 2012-02-15 11:07  Moon_1st  阅读(340)  评论(0编辑  收藏  举报

导航