Escaping the Hounds
Alice has wandered into the hedge maze in Bob's garden, and Bob has let his hounds loose. The maze
is a grid of n rows and m columns. Every second, Alice moves to one of the four side-adjacent
cells, and so does each hound (a hound may also stay put). Nobody can enter a hedge. The hounds are
clever and know Alice's whole plan before she starts, so if any hound can be standing on a cell no
later than Alice gets there, she will be caught. Alice is safe the moment she stands on a cell on the
edge of the maze, because from there she simply climbs out. Help Alice find the fastest way out, or
tell her that escape is impossible.
Formally, for a free cell x let h(x) be the length of the shortest path from any hound's
starting cell to x that moves between side-adjacent free cells (h(x) = 0 on a hound's starting
cell, and h(x) = ∞ if no hound can reach x at all). An escape route of length k is a sequence
of cells c_0, c_1, …, c_k such that:
c_0is Alice's starting cell;- each
c_iis a free cell, andc_{i-1}andc_iare side-adjacent for every1 ≤ i ≤ k; i < h(c_i)for every0 ≤ i ≤ k, i.e. Alice reaches every cell of her route strictly before any hound could;c_klies in the first row, the last row, the first column or the last column.
Find an escape route of minimum length, or report that none exists.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1000).
Each of the next n lines contains a string of exactly m characters describing a row of the
maze:
.is a free cell;#is a hedge;Ais Alice's starting cell (a free cell);His a hound's starting cell (a free cell).
There is exactly one A. There can be any number of H, including none.
Output
If Alice cannot escape, print NO.
Otherwise, print YES on the first line and the minimum length k of an escape route on the second
line. If k > 0, print the route on the third line as a string of k characters, where L, R,
U and D move Alice one cell left, right, up and down respectively. If k = 0, print nothing
after k.
If there are several shortest escape routes, print any of them.
Notes
In the first example, the top exit is right next to the hound, so Alice runs for the bottom exit: right twice and down three times. The hound needs 7 seconds to get there, and on every cell of her route Alice is at least 2 seconds ahead of it.
In the second example, Alice would reach the cell below the exit after 2 seconds, but the hound gets there after 2 seconds too, and that is enough to catch her.