TopCoder problem "HappyCells" used in SRM 406 (Division II Level One)



Problem Statement

    Consider a rectangular grid of cells that is given by a String[] grid. A cell marked with a 'X' character denotes an occupied cell, and a cell marked with a '.' character denotes an empty cell. We say that two cells are orthogonal neighbors if they share a common side, and they are diagonal neighbors if they are adjacent to one another diagonally.



We say that a cell is 1-happy if the cell is empty and all of the cell's orthogonal and diagonal neighbors are occupied (note that a cell may have fewer than 8 neighbors). A cell is 2-happy if the cell is empty and all of the cell's orthogonal neighbors are occupied, but one or more of its diagonal neighbors are empty. A cell is 3-happy if the cell is empty and all of the cell's diagonal neighbors are occupied, but one or more of its orthogonal neighbors are empty.



Return a int[] with 3 elements. The first element should be the number of 1-happy cells, the second element should be the number of 2-happy cells, and the third element should be the number of 3-happy cells.
 

Definition

    
Class:HappyCells
Method:getHappy
Parameters:String[]
Returns:int[]
Method signature:int[] getHappy(String[] grid)
(be sure your method is public)
    
 

Constraints

-grid will contain between 1 and 50 elements, inclusive.
-Each element of grid will contain between 1 and 50 characters, inclusive.
-Each element of grid will contain the same number of characters.
-Each character in grid will be either an uppercase 'X' or '.'
 

Examples

0)
    
{
"XXX",
"X.X",
"XXX"
}
Returns: {1, 0, 0 }
The center cell is 1-happy.
1)
    
{"."}
Returns: {1, 0, 0 }
Note that even though this cell has no neighbors, it is 1-happy because there are no neighbors to be empty.
2)
    
{
"XXXXXX",
"X.XXXX",
"XXX.XX",
"X..XXX",
"XXXXXX"
}
Returns: {1, 1, 1 }
The uppermost empty cell is 1-happy, the empty cell on the third row is 2-happy, and the left cell on the fourth row is 3-happy. Note that the right cell on the fourth row is not happy because it has both diagonal and orthogonal neighbors that are empty.
3)
    
{"..."}
Returns: {0, 0, 3 }
Note that with no diagonal neighbors, there are no diagonal neighbors to be empty. Thus, each cell is 3-happy.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=9770

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=12178&pm=9770

Writer:

eleusive

Testers:

PabloGilberto , Olexiy , gawry , ivan_metelsky

Problem categories:

Simple Search, Iteration