#K3091. Path in Grid: Determining a Valid Route
Path in Grid: Determining a Valid Route
Path in Grid: Determining a Valid Route
You are given a 2D grid of integers with dimensions \(N \times M\). Each cell in the grid is either 1 or 0, where 1 indicates an open space and 0 indicates an obstacle.
Your task is to determine whether there exists a path from the top-left corner (cell \((1,1)\)) to the bottom-right corner (cell \((N,M)\)) moving only in the four cardinal directions (up, down, left, right). The path can only traverse cells with value 1.
For example, consider the grid below:
1 0 1 1 1 1 1 1 0 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 1
The answer for this grid is "YES" since a valid path exists. Otherwise, output "NO" if no such path exists.
inputFormat
The first line contains two integers \(N\) and \(M\), representing the dimensions of the grid.
The next \(N\) lines each contain \(M\) space-separated integers (either 0 or 1) representing the grid.
outputFormat
Output a single line containing either "YES" if there is a valid path from the top-left to the bottom-right cell, or "NO" if there is not.
## sample5 5
1 0 1 1 1
1 1 1 0 1
0 1 0 0 1
1 1 1 1 0
1 0 0 1 1
YES
</p>