#K14736. Reach the Destination
Reach the Destination
Reach the Destination
John is trying to reach his destination in a grid. The grid has N rows and M columns, and each cell is either accessible or blocked. An accessible cell is denoted by the character O
while a blocked cell is denoted by X
. John starts at the top‐left cell (1, 1) and his destination is the bottom‐right cell (N, M). He can move one step at a time in the four cardinal directions: up, down, left, or right.
Your task is to determine whether John can reach his destination from the starting cell.
The grid can be mathematically represented as a matrix \(A\) of size \(N \times M\). John is allowed to move from cell \(A[i][j]\) to cell \(A[i'][j']\) if and only if \(|i-i'| + |j-j'| = 1\) and \(A[i'][j'] = O\).
Print "YES" if John can reach the destination, otherwise print "NO".
inputFormat
The input is given from stdin and consists of:
- The first line contains two space-separated integers \(N\) and \(M\), representing the number of rows and columns respectively.
- The next \(N\) lines each contain a string of length \(M\) that represents a row of the grid. Each character is either
O
(accessible) orX
(blocked).
outputFormat
Output a single line to stdout containing either "YES" if John can reach the destination (cell \((N, M)\)) from the starting point (cell \((1, 1)\)); otherwise, output "NO".
## sample3 4
OOOO
OXOX
OOOO
YES
</p>