#C8154. Grid Journey with Magic Portals
Grid Journey with Magic Portals
Grid Journey with Magic Portals
You are given an n x m grid. Each cell of the grid is either empty (denoted by '.'), blocked (denoted by '#'), or contains a magic portal (denoted by 'P'). A traveler starts from the top-left corner (cell (0,0)) and must reach the bottom-right corner (cell (n-1, m-1)). The traveler can move in four directions (up, down, left, right) to adjacent cells as long as they are not blocked. Additionally, when the traveler lands on a cell with a magic portal, they can instantly jump to any other cell in the grid that also contains a magic portal. The problem is to determine whether it is possible to reach the destination following these rules.
The input format is as follows: the first line contains two integers n and m. The next n lines each contain a string of length m representing a row of the grid.
The output should be YES
if the destination can be reached, or NO
otherwise.
inputFormat
The first line of input contains two space-separated integers n and m — the number of rows and columns of the grid respectively.
The next n lines each contain a string of length m consisting of the characters .
(empty cell), #
(blocked cell), and P
(magic portal).
outputFormat
Print a single line containing YES
if it is possible to reach the bottom-right cell from the top-left cell using the allowed moves and portal jumps. Otherwise, print NO
.
5 5
.....
.###.
.#P#.
.#P#.
.....
YES