TopCoder problem "DonutsOnTheGridEasy" used in Member SRM 455 (Division I Level One)



Problem Statement

    

Petya likes donuts. He tries to find them everywhere. For example if he is given a grid where each cell contains a '0' (zero) or '.' he will construct donuts from the cells.

To make the donuts:

  1. Petya first selects a rectangle of cells of width, w, and height, h, where both are at least 3.
  2. Then he removes a rectangular hole of width w-2 and height h-2 centered in the larger rectangle.
  3. For the remaining cells (a closed rectangular ring) to be a valid donut, all of the cells must contain '0' (because donuts are shaped like zeros). Cells in the hole can contain anything since they are not part of the donut.

An example of large donut containing a smaller donut.

..........
.00000000.
.0......0.
.0.0000.0.
.0.0..0.0.
.0.0..0.0.
.0.0000.0.
.0......0.
.00000000.
..........

Donuts may contain other donuts and donuts may touch, but the cells of one donut may not overlap the cells of another. Petra is most interested in donuts that contain other donuts. He first places one valid donut on the grid (if possible). He then places a valid donut in the hole of the first donut (if possible). He continues to place a donut into the hole of the most recently placed donut until this is no longer possible.

Your task is to find the maximum number of donuts that Petya can place on the grid as described such that each donut (except for the first) is contained within the previous donut. You are given grid, a String[], containing only '0' and '.' characters. The j-th character of the i-th element is the value at cell (i, j).

 

Definition

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

Constraints

-grid will have between 1 and 50 elements, inclusive.
-Each element of grid will have between 1 and 50 characters, inclusive.
-All elements of grid will have the same length.
-Each character of grid will be either '0' (zero) or '.'.
 

Examples

0)
    
{"0000000"
,"0.....0"
,"0.00000"
,"0.0..00"
,"0.00000"
,"0.....0"
,"0000000"}
Returns: 2
1)
    
{"000"
,"0.0"
,"000"}
Returns: 1
2)
    
{"..."
,"..."
,"..."}
Returns: 0
3)
    
{"00.000"
,"00.000"
,"0.00.0"
,"000.00"}
Returns: 0
4)
    
{"0000000....",
 "0000000....",
 "0000000.000",
 "0000000.0.0",
 "0000000.000",
 "0000000....",
 "0000000...."}
Returns: 3

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14179&pm=10719

Writer:

K.A.D.R

Testers:

Rustyoldman , timmac , StevieT , maksay , K.A.D.R , Seyaua

Problem categories:

Dynamic Programming, Simple Search, Iteration