Escaping the Hounds

medium graphs bfs shortest-paths 1000 ms · 256 MB

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_0 is Alice's starting cell;
  • each c_i is a free cell, and c_{i-1} and c_i are side-adjacent for every 1 ≤ i ≤ k;
  • i < h(c_i) for every 0 ≤ i ≤ k, i.e. Alice reaches every cell of her route strictly before any hound could;
  • c_k lies 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;
  • A is Alice's starting cell (a free cell);
  • H is 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.

Any correct answer is accepted.

Example 1

Input

7 7
###.###
#..H..#
#.###.#
#..A..#
#.###.#
#.....#
#####.#

Output

YES
5
RRDDD

Example 2

Input

3 7
###.###
#A...H#
#######

Output

NO

Hints · reveal one at a time if you're stuck

Hint 1

Alice's safety depends only on when each cell could first be reached by any hound, not on which hound. For every free cell, what is the earliest moment a hound can stand there, and how can you compute that for all cells at once?

Hint 2

Once you know the earliest hound time for each cell, run a second BFS from Alice. Enter a neighbouring cell only if Alice would arrive there strictly before the hounds, and stop at the first edge cell you reach. Breadth-first order makes that first edge cell the shortest escape.

Hint 3

To print the route, store for each visited cell the direction you entered it from, walk back from the exit to Alice, and reverse the moves. The grid can be 1000 by 1000, so do the walk-back iteratively rather than recursively.

report an issue Prefer your own editor? sooks prep 51 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.