#K39237. Count Safe Zones
Count Safe Zones
Count Safe Zones
In this problem, you are given a grid of size . Each cell in the grid is either an empty cell represented by a dot ('.') or an obstacle represented by an asterisk ('*'). Two cells are considered connected if they are adjacent vertically or horizontally. Your goal is to determine the number of connected components (safe zones) consisting entirely of empty cells. This is a classic flood fill / DFS problem in grid graphs.
The problem can be formulated as follows:
Given an grid, count the number of connected regions where each region is formed by adjacent cells (up, down, left, right) having a dot ('.') character.
For example, in the grid below:
.*..* *..** .*..* ***.*
There are 3 separate connected zones of empty cells.
Note: Use DFS or BFS to solve this problem effectively.
inputFormat
Input is read from standard input (stdin). The first line contains two integers and () representing the number of rows and columns. Each of the following lines contains a string of characters. Each character is either '.' (an empty cell) or '*' (an obstacle).
outputFormat
Output to standard output (stdout) a single integer representing the number of connected components (safe zones) of empty cells found in the grid.## sample
4 5
.*..*
*..**
.*..*
***.*
3