#K42887. Tom and Jerry's Chase Problem
Tom and Jerry's Chase Problem
Tom and Jerry's Chase Problem
You are given an N × M grid. Each cell of the grid is either free (denoted by '.') or blocked (denoted by '#'). Tom starts at the top-left cell (0,0) and wants to reach the bottom-right cell \( (N-1, M-1) \) by moving only right (\( (i, j) \to (i, j+1) \)) or down (\( (i, j) \to (i+1, j) \)). Meanwhile, Jerry wants to escape by moving from the bottom-right cell to the top-left cell. However, since Jerry can only move left and up, we can transform his problem into a similar one by reversing the grid. In short, if both Tom and the reversed version of Jerry can reach the destination following right and down moves, then output YES
; otherwise, output NO
.
Note: To simulate Jerry's movement (up and left), you can create a reversed grid by reversing the order of rows and, for each row, reversing the characters. Then, use the same right/down movement strategy to check if a path exists from the new top-left to the new bottom-right.
inputFormat
The first line contains two integers N
and M
separated by a space, representing the number of rows and columns of the grid, respectively. The following N
lines each contain a string of length M
representing a row of the grid.
For example:
3 4 ..#. ...# #...
outputFormat
Output a single line containing YES
if both Tom and Jerry can reach their destinations, or NO
otherwise.
3 4
..#.
...#
#...
YES