#K11376. Connectivity of Open Cells
Connectivity of Open Cells
Connectivity of Open Cells
You are given a grid with n rows and m columns. Each cell in the grid is either open (denoted by a '.') or blocked (denoted by a '#'). Your task is to determine whether all open cells in the grid form a single connected component. Two open cells are considered connected if they are adjacent horizontally or vertically.
If there is at least one open cell and all open cells are reachable from one another, output YES
. Otherwise, output NO
. Note that if there are no open cells in the grid, the answer should be NO
.
The connectivity condition must be verified using common graph traversal techniques such as BFS or DFS.
The mathematical condition for connectivity can be formally described as follows: Let \( S \) be the set of coordinates \((i,j)\) such that the cell \( grid[i][j] = '.' \). There exists a starting cell \( s \in S \) such that if \( R(s) \) is the set of cells reachable from \( s \) using only steps in the four cardinal directions (up, down, left, right) and remaining within \( S \), then the grid is fully connected if and only if \( R(s) = S \).
inputFormat
The first line of input contains two integers n and m (the number of rows and columns respectively). The following n lines each contain a string of length m representing the grid. Each character is either '.' (an open cell) or '#' (a blocked cell).
Input is provided via standard input (stdin).
outputFormat
Output a single line containing either YES
if all open cells are connected, or NO
otherwise. The output should be written to standard output (stdout).
4 5
.....
..#..
.###.
.....
YES
</p>