[usaco]5.4.5 Big Barn題解

5
 
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

 

--------------------------------------------------------------------------------

以前做過類似的題,所以這次特別的快。
算法很簡單,記錄每一個節點向左上方能夠延伸出來的最大的方塊的大小,以及能夠向左延伸的最大距離,還有向上延伸的最大距離。
這樣一來,如果某個節點是樹木,那麼這個節點的2個變量置0,否則left和up分別是上一個節點累加1,而squre取左上方一個節點+1,和left還有up相比較,取最小值,就是當前節點的squre。

還有個節省空間的辦法,left,up還有squre三個矩陣並不需要n×n,因爲有些行用過之後就沒有用了,因此只需要保存兩行就行了。

USER: Ma yunlei [yunleis2]
TASK: bigbrn
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4024 KB]
   Test 2: TEST OK [0.000 secs, 4024 KB]
   Test 3: TEST OK [0.000 secs, 4024 KB]
   Test 4: TEST OK [0.000 secs, 4024 KB]
   Test 5: TEST OK [0.000 secs, 4024 KB]
   Test 6: TEST OK [0.000 secs, 4024 KB]
   Test 7: TEST OK [0.000 secs, 4024 KB]
   Test 8: TEST OK [0.000 secs, 4024 KB]
   Test 9: TEST OK [0.000 secs, 4024 KB]
   Test 10: TEST OK [0.027 secs, 4024 KB]
   Test 11: TEST OK [0.027 secs, 4024 KB]
   Test 12: TEST OK [0.027 secs, 4024 KB]
   Test 13: TEST OK [0.027 secs, 4024 KB]
   Test 14: TEST OK [0.027 secs, 4024 KB]
   Test 15: TEST OK [0.027 secs, 4024 KB]

All tests OK.
YOUR PROGRAM ('bigbrn') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

---------------------------------------------------------------------------

/*
ID:yunleis3
PROG:bigbrn
LANG:C++
*/

#include <fstream>
#include<iostream>

using namespace std;
const int maxn=1001;
const int maxt=10001;
bool metri[maxn][maxn];
int up[2][maxn];
int left1[2][maxn];
int squr[2][maxn];
int n,t;
int large=0;
#define rowindex(row)  ((row)%2)
#define min(x,y)   (x<y?x:y)
#define trimin(x,y,z)  (min(min(x,y),z))
int main()
{
	ifstream fin("bigbrn.in");
	fin>>n>>t;
	for(int i=0;i<t;++i){
		int a,b;
		fin>>a>>b;
		metri[a][b]=true;
	}
	for(int row=1;row<=n;++row){
		for(int col=1;col<=n;++col){
			if(metri[row][col]){
				up[rowindex(row)][col]=0;
				left1[rowindex(row)][col]=0;
				squr[rowindex(row)][col]=0;
			}else {
				up[rowindex(row)][col]=up[rowindex(row-1)][col]+1;
				left1[rowindex(row)][col]=left1[rowindex(row)][col-1]+1;
				squr[rowindex(row)][col]=trimin(up[rowindex(row)][col],left1[rowindex(row)][col],squr[rowindex(row-1)][col-1]+1);
				if(large<squr[rowindex(row)][col])
					large=squr[rowindex(row)][col];
			}
		}
	}
	ofstream fout("bigbrn.out");
	fout<<large<<endl;
	//system("pause");
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章