TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Fishin

Problem statement, definition, constraints, and public examples.

Problem Statement

PROBLEM STATEMENT

Dynamite fishing is a destructive (and illegal in most places) method of
fishing where a fisherman will drop dynamite into the water to stun nearby
fish, and then collect the stunned fish as they float to the surface.  You are
given a map of a small (rectangular) lake, and you know that dynamite placed at
a location in the lake will kill (and destroy) all fish at that location, but
successfully stun all fish that are in adjacent (including diagonally adjacent)
locations.

Create a class Fishin that contains the method maxFish, which takes a String[]
lake and returns the maximum number of fish that can be stunned by a single
stick of dynamite dropped somewhere in the lake.  Each character in each
element of lake represents a small piece of the lake using a hash mark ('#') to
mark a fish and a tilde ('~') to mark empty water.  Consecutive characters in
each element represent east-west adjacent areas of the lake (for example, the
3rd character of the 1st element is just east of the 2nd character of the 1st
element).  Characters in equivalent positions of consecutive strings are
north-south adjacent (for example, the 3rd character of the 4th element is just
north of the 3rd character in the 5th element).  See the examples for further
clarification.

DEFINITION
Class: Fishin
Method: maxFish
Parameters: String[]
Returns: int
Method signature (be sure your method is public):  int maxFish(String[] lake);


NOTES
- Never count a fish in the same location as the dynamite - that fish, if
present, is destroyed and unusable.
- The dynamite MUST be thrown within the bounds of the lake, not off an edge.


TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- lake will contain between 1 and 50 elements, inclusive.
- Each element of lake will be between 1 and 50 characters in length, inclusive.
- All elements of lake will be the same length.
- Each element of lake will contain only hash marks ('#') and tildes ('~').


EXAMPLES
In the following examples, quotes, commas and braces are for clarity only.
They are never part of the input.

(1)
lake = {"~~~~~~~~","~~~#~~~~","~~~~##~~","~~~~~~~~"}
returns: 3

The lake looks like this:

        ~~~~~~~~
        ~~~#~~~~
        ~~~~##~~
        ~~~~~~~~

The fisherman can stun all 3 fish and kill none if he tosses the dynamite on
the '+' shown below:
        ~~~~~~~~
        ~~~#+~~~
        ~~~~##~~
        ~~~~~~~~

(2)
lake = {"~~~~~~~~","~~~###~~","~~~###~~","~~~###~~","~~~~~~~~"}
returns: 8

The lake looks like this:

        ~~~~~~~~
        ~~~###~~
        ~~~###~~
        ~~~###~~
        ~~~~~~~~

The fisherman can throw the dynamite in the center of the group and kill the
center one, but stun the other 8 if he tosses the dynamite on the '+' shown
below:

        ~~~~~~~~
        ~~~###~~
        ~~~#+#~~
        ~~~###~~
        ~~~~~~~~


(3)
lake = {"#"}
returns: 0

There is nowhere he can throw the dynamite without killing this one fish.


(4)
lake =
{"########","########","########","########","########","########","########","#
#######"}
returns: 8


(5)
lake =
{"~~~~~~~~","~~~~~~~~","~~~~~~~~","~~~~~~~~","~~~~~~~~","~~~~~~~~","~~~~~~~~","~
~~~~~~~"}
returns: 0

Definition

Class:
Fishin
Method:
maxFish
Parameters:
String[]
Returns:
int
Method signature:
int maxFish(String[] param0)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems