Connection Status:
Competition Arena > DonutsOnTheGridEasy
Member SRM 455 · 2009-12-03 · by K.A.D.R · Dynamic Programming, Simple Search, Iteration
Class Name: DonutsOnTheGridEasy
Return Type: int
Method Name: calc
Arg Types: (vector<string>)
Problem Statement

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).

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)
{"0000","0000","0000","0000"}
Returns: 1
4)
{"000000","000000","0000.0","000000"}
Returns: 1

Submissions are judged against all 164 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class DonutsOnTheGridEasy with a public method int calc(vector<string> grid) · 164 test cases · 2 s / 256 MB per case

Submitting as anonymous