#K40107. All Players Reach Target
All Players Reach Target
All Players Reach Target
Given an \(m \times n\) grid representing a game board, each cell can contain one of the following characters:
- 'P': a player
- 'T': the target
- 'X': an obstacle that cannot be passed
- '.': an empty cell
A player can move to an adjacent cell (up, down, left, or right) if that cell is empty ('.'), a player ('P'), or the target ('T'). Note that players do not block each other. Your task is to determine whether each player in the grid can reach the target cell. If there are no players in the grid, output YES
.
It is recommended to use a breadth-first search (BFS) or similar algorithm to solve this problem. All formulas are written in LaTeX format.
inputFormat
The first line of input contains two integers \(m\) and \(n\) which denote the number of rows and columns of the grid, respectively. This is followed by \(m\) lines, each containing a string of length \(n\) that describes a row of the grid.
outputFormat
Output a single line containing YES
if every player can reach the target; otherwise, output NO
.
4 5
P....
..X..
..XT.
P....
YES