#P4668. Treasure Route Planning
Treasure Route Planning
Treasure Route Planning
You are given an N × M grid map representing sea and islands. Certain cells contain your starting position (S), the treasure (T), and an enemy Viking ship (V). All of these occupy sea cells (represented by S, T, V or .), while islands are represented by #.
Your task is to plan a fixed route from S to T by moving only horizontally or vertically to an adjacent sea cell. The route must be determined in advance. After each move you make, an adversarial Viking ship can move (or not move) one cell (again, horizontally or vertically) on any sea cell reachable by its moves. Each round consists of your move followed by the Viking’s move.
After every round, a safety check is performed: if you are in the same row or the same column as the Viking ship and there is a clear line (i.e. only sea cells, no islands) between you and the Viking ship, then you are caught and killed. Note that since the Viking ship may choose its move adversarially, any possibility of such a clear line of sight renders the route unsafe.
Formally, if you are at cell p = (i,j) at round t (starting with t = 0 at the starting position) and the Viking ship is in any cell v on the same row or column as p with all cells between them being sea, and if the minimum number of moves needed for the Viking ship to reach v is at most t, then that round is unsafe.
Determine if there exists a fixed route from S to T that is guaranteed to be safe at every round, regardless of how the Viking ship moves.
inputFormat
The first line contains two integers N and M denoting the number of rows and columns.
The following N lines each contain exactly M characters. The characters can be:
- S: Your starting position (sea cell).
- T: The treasure (sea cell).
- V: The Viking ship’s starting position (sea cell).
- .: Sea.
- #: Island.
outputFormat
Output a single line containing "YES" if there exists a safe fixed route from S to T, or "NO" otherwise.
sample
3 3
S.V
.#.
..T
YES