#K95032. Path Existence in a Grid
Path Existence in a Grid
Path Existence in a Grid
You are given a grid of size \(n \times m\) where each cell is either 'O' (open) or 'X' (blocked). Your goal is to determine whether there exists a path from the top-left corner (cell (1,1)) to the bottom-right corner (cell (n,m)).
You can move one step at a time in any of the four directions: up, down, left, or right. A valid path can only step on cells marked with 'O'. If the starting cell or the ending cell is blocked ('X'), then the answer is immediately "NO".
Note: The movement is defined by the equations:
\[ (i, j) \rightarrow (i+1, j), \quad (i, j) \rightarrow (i-1, j), \quad (i, j) \rightarrow (i, j+1), \quad (i, j) \rightarrow (i, j-1) \]
Determine if there exists any such path. Output "YES" if there is a path, and "NO" otherwise.
inputFormat
The input is read from standard input (stdin). The first line contains two integers (n) and (m) (the number of rows and columns of the grid, respectively). This is followed by (n) lines, each containing a string of length (m) representing the grid. Each character in the string is either 'O' (open) or 'X' (blocked).
outputFormat
Output a single line to standard output (stdout) with "YES" if there exists a valid path from the top-left corner to the bottom-right corner, or "NO" if there is no such path.## sample
3 3
OXO
OXO
OOO
YES