import
java.util.*;
public
class
Main {
static
final
int
LARGEST =
10000000
;
static
List<List<Integer>> g;
static
List<Pair<Integer, Integer>> M;
static
List<List<Pair<Pair<Integer, Integer>, Integer>>> parent;
static
int
sx, sy;
static
final
int
[] dx = {
1
,
0
, -
1
,
0
};
static
final
int
[] dy = {
0
, -
1
,
0
,
1
};
static
int
n, m, ex = -
1
, ey = -
1
;
public
static
void
main(String[] args) {
List<List<Character>> mat = Arrays.asList(
Arrays.asList(
'#'
,
'M'
,
'.'
),
Arrays.asList(
'#'
,
'A'
,
'M'
),
Arrays.asList(
'#'
,
'.'
,
'#'
)
);
n = mat.size();
m = mat.get(
0
).size();
g =
new
ArrayList<>(n +
1
);
for
(
int
i =
0
; i <= n; i++) {
g.add(
new
ArrayList<>(Collections.nCopies(m +
1
,
0
)));
}
parent =
new
ArrayList<>(n +
1
);
for
(
int
i =
0
; i <= n; i++) {
parent.add(
new
ArrayList<>(Collections.nCopies(m +
1
,
new
Pair<>(
new
Pair<>(-
1
, -
1
), -
1
))));
}
M =
new
ArrayList<>();
for
(
int
i =
1
; i <= n; i++) {
for
(
int
j =
1
; j <= m; j++) {
parent.get(i).get(j).first.first = -
1
;
parent.get(i).get(j).first.second = -
1
;
parent.get(i).get(j).second = -
1
;
char
x = mat.get(i -
1
).get(j -
1
);
if
(x ==
'M'
) {
M.add(
new
Pair<>(i, j));
g.get(i).set(j,
0
);
}
else
if
(x ==
'A'
) {
sx = i;
sy = j;
g.get(i).set(j, -
1
);
}
else
if
(x ==
'.'
) {
g.get(i).set(j, -
1
);
}
else
{
g.get(i).set(j, LARGEST +
1
);
}
}
}
if
(isborder(sx, sy)) {
System.out.println(
"Already a boundary cell"
);
}
else
{
fillMatrix();
int
time = bfs();
if
(ex == -
1
) {
System.out.println(ex);
}
else
{
List<Character> ans =
new
ArrayList<>();
while
(!(ex == sx && ey == sy)) {
int
x = parent.get(ex).get(ey).first.first;
int
y = parent.get(ex).get(ey).first.second;
char
dir = cal(x, y, ex, ey);
ans.add(dir);
ex = x;
ey = y;
}
Collections.reverse(ans);
for
(
char
x : ans) {
System.out.print(x);
}
}
}
}
static
boolean
isvalid(
int
x,
int
y) {
if
(x <
1
|| x > n || y <
0
|| y > m) {
return
false
;
}
if
(g.get(x).get(y) == LARGEST +
1
) {
return
false
;
}
return
true
;
}
static
boolean
isborder(
int
x,
int
y) {
return
x ==
1
|| y ==
1
|| x == n || y == m;
}
static
char
cal(
int
x,
int
y,
int
ex,
int
ey) {
if
(x +
1
== ex && y == ey) {
return
'D'
;
}
if
(x -
1
== ex && y == ey) {
return
'U'
;
}
if
(x == ex && y +
1
== ey) {
return
'R'
;
}
return
'L'
;
}
static
void
fillMatrix() {
Queue<Pair<Pair<Integer, Integer>, Integer>> q =
new
LinkedList<>();
for
(Pair<Integer, Integer> m : M) {
q.add(
new
Pair<>(m,
0
));
g.get(m.first).set(m.second,
0
);
}
while
(!q.isEmpty()) {
int
x = q.peek().first.first;
int
y = q.peek().first.second;
int
time = q.peek().second;
q.poll();
for
(
int
i =
0
; i <
4
; i++) {
int
newx = x + dx[i];
int
newy = y + dy[i];
if
(!isvalid(newx, newy)) {
continue
;
}
if
(g.get(newx).get(newy) == -
1
) {
g.get(newx).set(newy, time +
1
);
q.add(
new
Pair<>(
new
Pair<>(newx, newy), time +
1
));
}
}
}
for
(
int
i =
1
; i <= n; i++) {
for
(
int
j =
1
; j <= m; j++) {
if
(g.get(i).get(j) == -
1
) {
g.get(i).set(j, LARGEST);
}
}
}
}
static
int
bfs() {
Queue<Pair<Pair<Integer, Integer>, Integer>> q =
new
LinkedList<>();
q.add(
new
Pair<>(
new
Pair<>(sx, sy),
0
));
while
(!q.isEmpty()) {
int
x = q.peek().first.first;
int
y = q.peek().first.second;
int
time = q.peek().second;
q.poll();
for
(
int
i =
0
; i <
4
; i++) {
int
newx = x + dx[i];
int
newy = y + dy[i];
if
(!isvalid(newx, newy)) {
continue
;
}
if
(time +
1
>= g.get(newx).get(newy)) {
continue
;
}
if
(parent.get(newx).get(newy).first.first != -
1
) {
continue
;
}
parent.get(newx).get(newy).first.first = x;
parent.get(newx).get(newy).first.second = y;
parent.get(newx).get(newy).second = time +
1
;
q.add(
new
Pair<>(
new
Pair<>(newx, newy), time +
1
));
if
(isborder(newx, newy)) {
ex = newx;
ey = newy;
return
time +
1
;
}
}
}
return
-
1
;
}
}
class
Pair<A, B> {
A first;
B second;
Pair(A first, B second) {
this
.first = first;
this
.second = second;
}
}