C++&Pascal&Python——【USACO 5.3.4】——Big Barn

44 篇文章 0 订阅
23 篇文章 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

大谷仓


农夫约翰想在他的农场上放一个大的方形谷仓。他不喜欢在他的农场里砍树,他想找一个空旷地方放他的谷仓,这样他就能在已经很清澈的土地上建造它。为了我们的目的,他的土地被分成了N * N块。输入包含树的列表。你的工作是确定和报告最大可能的方形谷仓,而不需要清除树木。谷仓边必须与水平或垂直轴平行。

例子

考虑下面的农民约翰的土地。' . '代表一个没有树的地方,‘#’代表了一个有树的地方:

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

最大的谷仓是5 x 5,可以放置在栅格的右下部分的两个位置。

项目名称:bigbrn

输入格式

第一行:两个整数:N(1 <= N <= 1000),边长N,和T(1 <= 10,000)

行2 . .T+ 1:两个整数(1 < =每个整数< = N),树的行和列

示例输入(文件bigbrn.in)

8 3
2 2
2 6
6 3

输出格式

输出文件应该包含一行,即John的谷仓的最大长度。

样例输出(文件bigbrn.out)

5



/*
ID : mcdonne1
LANG : C++
TASK : bigbrn
*/

#pragma GCC optimize("O3")

#include <iostream>
#include <fstream>

using namespace std;

int n, t, x, y, ans;
int f[1001][1001];

int main () {
	ifstream fin ("bigbrn.in", ios::in);
	ofstream fout ("bigbrn.out", ios::out);
	fin>>n>>t;
	for (int i = 1; i <= t; i++) {
		fin>>x>>y;
		f[x][y] = ~0;
	}
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if(f[i][j] ^ -1)
				f[i][j] = max (1, min (min (f[i - 1][j - 1] + 1, f[i][j - 1] + 1), min (f[i - 1][j - 1] + 1, f[i - 1][j] + 1)));
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (f[i][j] > ans) ans = f[i][j];
	fout<<ans<<endl;
	return 0;
}

{
ID : mcdonne1
LANG : PASCAL
TASK : bigbrn
}

uses math;
var
n, t, x, y, i, j, ans : integer;
f : array [0..1000, 0..1000] of integer;
begin
        assign (input, 'bigbrn.in');
        assign (output, 'bigbrn.out');
        reset (input);
        rewrite (output);
        read (n, t);
        for i := 1 to t do begin
                read (x, y);
                f[x, y] := not 0;
        end;
        for i := 1 to n do
                for j := 1 to n do
                        if f[i, j] <> -1 then
                                f[i, j] := max (1, min (min (f[i - 1, j - 1] + 1, f[i, j - 1] + 1), min (f[i - 1, j - 1] + 1, f[i - 1, j] + 1)));
        for i := 1 to n do
                for j := 1 to n do
                        if f[i, j] > ans then
                                ans := f[i, j];
        writeln (ans);
        close (input);
        close (output);
end.

'''
ID : mcdonne1
LANG : PYTHON2
TASK : bigbrn
'''
fin = open ('bigbrn.in', 'r')
fout = open ('bigbrn.out', 'w')
f = [[0 for i in range (1001)] for j in range (1001)]
r = fin.readline().split()
n = int(r[0])
t = int(r[1])
for i in range (t) :
	r = fin.readline().split()
	f[int(r[0])][int(r[1])] = -1
for i in range (1, n + 1) :
	for j in range (1, n + 1) :
		if f[i][j] != -1 :
			f[i][j] = max (1, 1 + min (f[i - 1][j - 1], f[i][j - 1], f[i - 1][j]))
ans = 0
for i in range (1, n + 1) :
	for j in range (1, n + 1) :
		if f[i][j] > ans :
			ans = f[i][j]
fout.write (str(ans) + '\n')
fin.close()
fout.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值