#C8240. Path Existence in a Binary Matrix
Path Existence in a Binary Matrix
Path Existence in a Binary Matrix
You are given an \(n \times m\) binary matrix where each cell is either 0 (blocked) or 1 (open). Starting from the top-left cell and moving only in four cardinal directions (up, down, left, right), determine whether there is a path to the bottom-right cell. The path may only traverse cells with value 1.
A valid path exists if and only if both the starting cell (top-left) and the destination cell (bottom-right) are open (i.e., equal to 1) and there is a sequence of moves connecting them. If a path exists, output "YES"; otherwise, output "NO".
Note: Diagonal moves are not allowed.
inputFormat
The first line contains two integers (n) and (m), representing the number of rows and columns, respectively. The following (n) lines each contain (m) integers (either 0 or 1) separated by spaces, which represent the binary matrix.
outputFormat
Output a single line containing "YES" if there exists a valid path from the top-left to the bottom-right cell, or "NO" otherwise.## sample
3 3
1 1 0
0 1 1
1 1 1
YES