#K55067. Path Finding in a Grid
Path Finding in a Grid
Path Finding in a Grid
You are given a grid composed of characters 'O' and 'X'. The character 'O'
represents an open cell and 'X'
represents a blocked cell. Your task is to determine whether there exists a path from the top-left corner (cell \( (0,0) \)) to the bottom-right corner (cell \( (n-1, m-1) \)). You may only move in four directions: up, down, left, and right, and you can only step on cells marked with 'O'
.
Note: The grid is guaranteed to have at least one row, and all rows have the same number of characters. The starting position (top-left) and the goal (bottom-right) are considered valid nodes only if they contain an 'O'
.
Example: For the grid
OXO OOX OOOthere exists a valid path from the top-left to the bottom-right, so the output is
YES
; whereas for the grid
OXO XXO OOOthe output is
NO
.
inputFormat
The input is read from standard input (stdin). The first line contains a single integer n representing the number of rows in the grid. The following n lines each contain a string of equal length representing a row of the grid. Each character is either O
(open) or X
(blocked).
For example:
3 OXO OOX OOO
outputFormat
The output should be printed on standard output (stdout). Print YES
if there is a valid path from the top-left corner to the bottom-right corner of the grid, or NO
if there is not.
3
OXO
OOX
OOO
YES