#135-(EZOI模拟赛)【DP】巨大的牛棚

版权声明:反正也没有人会转,下一个 https://blog.csdn.net/drtlstf/article/details/83216741

Description

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

1 2 3 4 5 6 7 8
1 . . . . . . . .
2 . # . . . # . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . # . . . . .
7 . . . . . . . .
8 . . . . . . . .

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

Input

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

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

Output

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

Sample Input

8 3
2 2
2 6
6 3

Sample Output

5

此处的条件转移方程:

if (a[i][j] is not ‘#’)

    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1;

else

    dp[i][j] = 0;

#include <iostream>

#define SIZE 1010

using namespace std;

int dp[SIZE][SIZE];
bool a[SIZE][SIZE];

int main(int argc, char** argv)
{
	int n, m, i, j, x, y, res = 1;
	
	scanf("%d%d", &n, &m);
	while (m--)
	{
		scanf("%d%d", &x, &y);
		a[x][y] = true;
	}
	
	for (i = 1; i <= n; ++i)
	{
		dp[1][i] = !a[1][i]; // 边界条件
		dp[i][1] = !a[i][1];
	}
	for (i = 2; i <= n; ++i)
	{
		for (j = 2; j <= n; ++j)
		{
			if (!a[i][j])
			{
				dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1])) + 1; // 条件转移方程
				res = max(res, dp[i][j]);
			}
		}
	}
	
	printf("%d", res);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/drtlstf/article/details/83216741
今日推荐