#K50697. Balanced Garden
Balanced Garden
Balanced Garden
You are given a garden represented as a grid with N
rows and M
columns. Each cell of the grid contains an integer:
0
represents a flower.1
represents a river.
A garden is considered balanced if for every flower (cell with 0
), there exists at least one adjacent cell (vertically or horizontally) that contains a river (1
). In mathematical terms, for every cell \( (i,j) \) with \( grid[i][j]=0 \), there must exist an adjacent cell \( (i',j') \) (where \( |i-i'|+|j-j'|=1 \)) such that \( grid[i'][j']=1 \).
Your task is to determine if the garden is balanced. Print YES
if it is balanced, otherwise print NO
.
inputFormat
The input is read from standard input and has the following format:
N M row1 row2 ... rowN
Here, the first line contains two integers N
and M
representing the number of rows and columns in the garden respectively. The next N
lines each contain M
space-separated integers (each either 0 or 1) representing the grid.
outputFormat
Output a single line to standard output: YES
if the garden is balanced according to the given definition, otherwise NO
.
3 3
0 1 0
1 0 1
0 1 0
YES