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
{"...", "...", "..."}Returns: 1
The whole map is a single large chamber.{"XX", "XX"}Returns: 0
With everything being walls, there are no chambers at all.{".X.", "X.X", ".X."}Returns: 5
Remember, only cells that are adjacent horizontally or vertically are part of the same chamber.{"X...", ".XX.", "...."}Returns: 1
Chambers can be weird shapes.{"X...", ".XX.", "..X."}Returns: 2
{"XXXXX.", "XXXXX.", "XX..X.", "X..XX.", "X.X.XX", "XX.X.."}Returns: 5