PROBLEM STATEMENT
Dots, crosses, and empty spaces are marked on squares of an m by n grid.
Squares that have a dot are marked with an 'o', squares that have a cross are
marked with an 'x', and squares that have empty spaces are marked with a '-'.
Given the layout of dots, crosses, and empty spaces on the grid, determine the
absolute value of the difference between the number of dots that are surrounded
by crosses and the number of crosses that are surrounded by dots. The
following test determines if a dot is surrounded by a cross: start at the
square occupied by the dot and try to get to a square on the edge of the grid
without encountering a cross; you are only allowed to move up, down, left, or
right. An analogous test determines if a cross is surrounded by a dot.
DEFINITION
Class: GridLocks
Method: difference
Parameters: String[]
Returns: int
Method signature (be sure your method is public): int difference(String[] grid);
TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
-grid contains between 1 and 10 elements, inclusive.
-Each element of grid has length between 1 and 10, inclusive.
-Each element of grid has the same length.
-The only characters allowed in an element of grid are: '-' 'x' 'o'.
EXAMPLES
Example 1: grid =
{"----------","---xxo----","--xooxo---","---xxo----","----------"}
---------- two dots are surrounded by the crosses and
---xxo---- one cross is surrounded by the dots
--xooxo--- |2 - 1| = 1. The method returns 1.
---xxo----
----------
Example 2: grid = {"-xox","-xxx","----"}
-xox zero dots are surrounded by crosses
-xxx zero crosses are surrounded by the dots
---- |0 - 0| = 0. The method returns 0.
Example 3: grid = {"oooooo","ooxxoo","oxooxo","ooxxox","ooooxo"}
oooooo three dots are surrounded by the crosses
ooxxoo six crosses are surrounded by the dots
oxooxo |3 - 6| = 3. The method returns 3.
ooxxox
ooooxo