TopcoderARCHIVE
Archive/Problems/ChamberCount
SRM · Problem 17431

ChamberCount

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a 2D grid of cells in String[] map. Each element of map repreents a single row of the grid, and each character of each element represents a single cell. An 'X' represents a wall, and a '.' represents an open cell.

We call a set of connected open cells a chamber. Any two '.' cells which are adjacent horizontally or vertically are part of the same chamber.

Return the number of distinct chambers represented in the map.

Definition

Class:
ChamberCount
Method:
count
Parameters:
String[]
Returns:
int
Method signature:
int count(String[] map)
(be sure your method is public)

Constraints

  • map will contain between 1 and 50 elements.
  • Each element of map will contain between 1 and 50 characters.
  • Each element of map will be the same length.
  • Each character of each element of map will be 'X' or '.'.

Examples

  1. {"...",
     "...",
     "..."}
    Returns: 1
    The whole map is a single large chamber.
  2. {"XX",
     "XX"}
    Returns: 0
    With everything being walls, there are no chambers at all.
  3. {".X.",
     "X.X",
     ".X."}
    Returns: 5
    Remember, only cells that are adjacent horizontally or vertically are part of the same chamber.
  4. {"X...",
     ".XX.",
     "...."}
    Returns: 1
    Chambers can be weird shapes.
  5. {"X...",
     ".XX.",
     "..X."}
    Returns: 2
  6. {"XXXXX.",
     "XXXXX.",
     "XX..X.",
     "X..XX.",
     "X.X.XX",
     "XX.X.."}
    Returns: 5
← All problems