TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

ColoringCount

Problem statement, definition, constraints, and public examples.

Problem Statement

You want to know how many distinct regions there are on a particular page in a color-by-number book. You will be given a String[] input representing the page we are examining. Each element of input will represent a horizontal section of the page. Each character in input will represent a space on the page. If a particular character in input is a (quotes for clarity) '.', then the space it represents is blank. If a particular character in input is a (quotes for clarity) '#', then the space it represents is part of a line. 2 spaces in input are in the same region if there is a path from one to the other consisting of only horizontal or vertical moves that never crosses a line. This path may not leave the page. In addition, spaces that belong to lines are never considered part of regions. For example:
input = {"...#...",
         "...#...",
         "#######",
         "...#...",
         "###...#"}
The page above has 4 regions. These regions are labeled below:
          111#222
          111#222
          #######
          333#444
          ###444#
Given a String[] input representing a page in the coloring book, return how many distinct regions there are. In the above example, your method would return 4.

Definition

Class:
ColoringCount
Method:
howMany
Parameters:
String[]
Returns:
int
Method signature:
int howMany(String[] input)
(be sure your method is public)

Constraints

  • input will contain between 1 and 20 elements inclusive
  • Each element of input will contain between 1 and 20 characters inclusive
  • Each element of input will contain the same number of characters
  • Each element of input will only contain (quotes for clarity) periods '.', or number-signs '#'

Examples

  1. {"...#...",
     "...#...",
     "#######",
     "...#...",
     "###...#"}
    Returns: 4
    Example from above.
  2. {"#"}
    Returns: 0
    A single line can't be a region.
  3. {"."}
    Returns: 1
    A single space is the only region.
  4. {"....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "....................",
     "...................."}
    Returns: 1
    One big space.
  5. {".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#",
     "#.#.#.#.#.#.#.#.#.#.",
     ".#.#.#.#.#.#.#.#.#.#", 
     "#.#.#.#.#.#.#.#.#.#."}
    
    Returns: 200
    A big checkerboard.
  6. {"...#...#...#"}
    Returns: 3
  7. {".",
     "#",
     ".",
     ".",
     "#",
     "."}
    Returns: 3
Back to all problems