C++&Pascal&Python——【USACO 5.3.4】——Big Barn
2017-10-25 16:54
477 查看
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.
The largest barn is 5 x 5 and can be placed in either of two locations in the lower right part of the grid.
大谷仓
农夫约翰想在他的农场上放一个大的方形谷仓。他不喜欢在他的农场里砍树,他想找一个空旷地方放他的谷仓,这样他就能在已经很清澈的土地上建造它。为了我们的目的,他的土地被分成了N * N块。输入包含树的列表。你的工作是确定和报告最大可能的方形谷仓,而不需要清除树木。谷仓边必须与水平或垂直轴平行。
例子
考虑下面的农民约翰的土地。' . '代表一个没有树的地方,‘#’代表了一个有树的地方:
最大的谷仓是5 x 5,可以放置在栅格的右下部分的两个位置。
项目名称:bigbrn
输入格式
第一行:两个整数:N(1 <= N <= 1000),边长N,和T(1 <= 10,000)
行2 . .T+ 1:两个整数(1 < =每个整数< = N),树的行和列
示例输入(文件bigbrn.in)
输出格式
输出文件应该包含一行,即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;
}
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()
相关文章推荐
- C++&Pascal&Python——【USACO 3.4.2】——Electric Fence
- C++&Pascal&Python——【USACO 4.2.2】——The Perfect Stall
- Pascal & C++——USACO Section 1.2 题解
- C++&Pascal——【USACO 5.1.1】——Fencing the Cows
- C++&Pascal——【USACO 3.3.1】——Riding the Fences
- C++&Pascal——【USACO 3.3.2】——Shopping Offers
- C++&Python——【USACO 5.4.1】——Canada Tour
- usaco 5.3.4 Big Barn
- splay详解(pascal&C++版)
- 118. Pascal's Triangle [easy] (Python)
- 从URDF到KDL(C++&Python)
- 算法导论-分治法-合并排序的Python&C++实现
- [ACM训练] 数据结构----树、二叉树----c++ && python
- 算法导论-排序的Python&C++简单实现
- Java & Pascal & C++——容斥原理例题——切蛋糕
- Pascal & C++——NOIP模拟题——小X学游泳
- 第十一周:( LeetCode567) Permutation in String(c++ && python)
- 119. Pascal's Triangle II [easy] (Python)
- AtCoder Beginner Contest 069 ABC C++&&Python
- && C++_USACO_Calf Flac