#K71512. Connected Component in Matrix
Connected Component in Matrix
Connected Component in Matrix
You are given a square matrix of size , where each cell contains either 0 or 1. Two cells containing 1 are considered adjacent if they share a horizontal or vertical edge. Your task is to determine whether all the 1s in the matrix form exactly one connected component. Note that if the matrix does not contain any 1, it is considered to satisfy this property.
In other words, if there is at least one cell with the value 1, then starting from one such cell, you should be able to reach every other cell with the value 1 by moving only up, down, left, or right. Otherwise, the answer is "NO".
inputFormat
The first line of input contains an integer , representing the dimensions of the matrix. The following lines each contain integers (each either 0 or 1) separated by spaces.
outputFormat
Output a single line with either "YES" if all the 1s form exactly one connected component (or if there are no 1s), or "NO" otherwise.## sample
4
1 0 0 1
1 1 0 1
0 1 1 0
0 0 0 0
NO