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

来源:互联网 发布:淘宝买家怎么修改价格 编辑:程序博客网 时间:2024/05/24 04:22

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 treesLines 2..T+1:Two integers (1 <= each integer <= N), the row and column of a tree parcel

SAMPLE INPUT (file bigbrn.in)

8 32 22 66 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 32 22 66 3

输出格式

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

样例输出(文件bigbrn.out)

5



/*ID : mcdonne1LANG : 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 : mcdonne1LANG : PASCALTASK : bigbrn}uses math;varn, 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 : mcdonne1LANG : PYTHON2TASK : 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])] = -1for 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 = 0for 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()