Problem Statement
PROBLEM STATEMENT
A farmer has both grazing land for livestock and crop-land for crops. He wants
to make sure he can use all of his grazing land for his animals, but that none
of the animals can get to his crops or get off his land.
His land is arranged a little oddly. His rectangular land is divided into
2-meter by 2-meter squares, each of which may be either crop-land or grazing
land.
You are given a map of his land, in the form of a String[], where each element
represents an east-west row of these 2-meter by 2-meter squares on his land
(see examples 1 and 2). Hash marks '#' represent grazing land, and periods
('.') represent crop-land. He wants to know how many fence posts are needed,
at a minimum, if a fencepost is needed to secure each end of every 1-meter
length of fence.
Create a class FencePosts that contains the method number, which takes a
String[] field - representing the map of the field - and returns the minimum
number of fence posts needed to keep the farmer's livestock on his own land and
out of his crops.
DEFINITION
Class: FencePosts
Method: number
Parameters: String[]
Returns: int
Method signature (be sure your method is public): int number(String[] field);
NOTES
- Posts can be shared by more than one length of fence.
- Single lengths of fence must always be straight, they can not curve or go
around corners. Any corner of a fence requires a fence post.
- No piece of crop-land may be accessible from any piece of grazing land.
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- fields will contain between 1 and 50 elements inclusive.
- Each element of field will contain between 1 and 50 characters inclusive.
- All elements of field will be the same length.
- Each element of field will contain only hashmarks ('#') and periods ('.').
EXAMPLES
In the following examples, quotes, commas and braces are for clarity only.
They are never part of the input.
(1)
field = {"....",".##.",".##.","...."}
returns: 16
This field looks like this:
....
.##.
.##.
....
And the 16 FencePosts would be arranged like this (enlarged - with 'O's being
fence posts and '-'s and '|'s being single lengths of fence):
... ... ... ...
... ... ... ...
... ... ... ...
O-O-O-O-O
...|### ###|...
...O### ###O...
...|### ###|...
O O
...|### ###|...
...O### ###O...
...|### ###|...
O-O-O-O-O
... ... ... ...
... ... ... ...
... ... ... ...
(2)
field = {"....",".#..","..#.","...."}
returns: 15
This field looks like this:
....
.#..
..#.
....
And the 15 FencePosts would be arranged like this (enlarged - with 'O's being
fence posts and '-'s and '|'s being single lengths of fence):
... ... ... ...
... ... ... ...
... ... ... ...
O-O-O
...|###|... ...
...O###O... ...
...|###|... ...
O-O-O-O-O
... ...|###|...
... ...O###O...
... ...|###|...
O-O-O
... ... ... ...
... ... ... ...
... ... ... ...
(3)
field = {".##.","####","####",".##."}
returns: 32
Don't forget about the outside edges in this one.Definition
- Class:
- FencePosts
- Method:
- number
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int number(String[] param0)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.