#include <bits/stdc++.h>
using namespace std;
struct Pos {
int r, c;
bool operator==(const Pos &o) const { return r == o.r && c == o.c; }
bool operator!=(const Pos &o) const { return !(*this == o); }
};
struct State {
Pos player, box;
int g, h;
bool operator>(const State &o) const { return g + h > o.g + o.h; }
};
struct HashState {
size_t operator()(const pair<Pos, Pos> &p) const {
return (p.first.r * 31 + p.first.c) * 131 + (p.second.r * 31 + p.second.c);
}
};
int R = 16, C = 16;
vector<string> grid;
Pos myPos, goalA, goalB;
vector<vector<bool>> isDeadCorner;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
char moveChar[4] = {'U', 'D', 'L', 'R'};
bool inBounds(int r, int c) {
return r >= 0 && r < R && c >= 0 && c < C;
}
bool isWall(int r, int c) {
return !inBounds(r, c) || grid[r][c] == '#' || grid[r][c] == 'b';
}
int manhattan(const Pos &a, const Pos &b) {
return abs(a.r - b.r) + abs(a.c - b.c);
}
void computeDeadCorners() {
isDeadCorner.assign(R, vector<bool>(C, false));
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
if (grid[r][c] == '#' || Pos{r,c} == goalA) continue;
bool up = isWall(r - 1, c), down = isWall(r + 1, c);
bool left = isWall(r, c - 1), right = isWall(r, c + 1);
if ((up && left) || (up && right) || (down && left) || (down && right)) {
isDeadCorner[r][c] = true;
}
}
}
}
// A* search: returns path string or ""
string aStarBox(Pos startPlayer, Pos startBox) {
priority_queue<State, vector<State>, greater<State>> pq;
unordered_map<pair<Pos, Pos>, pair<Pos, Pos>, HashState> parent;
unordered_map<pair<Pos, Pos>, char, HashState> moveTaken;
// Lưu khoảng cách tốt nhất từng gặp từ box đến A
unordered_map<pair<Pos, Pos>, int, HashState> bestDistToGoal;
auto heuristic = [&](const Pos &box) {
return manhattan(box, goalA) * 3;
};
auto boxDist = [&](const Pos &b) {
return manhattan(b, goalA);
};
pq.push({startPlayer, startBox, 0, heuristic(startBox)});
bestDistToGoal[{startPlayer, startBox}] = boxDist(startBox);
while (!pq.empty()) {
State cur = pq.top(); pq.pop();
if (cur.box == goalA) {
// reconstruct
string path;
pair<Pos, Pos> key = {cur.player, cur.box};
while (parent.count(key)) {
path.push_back(moveTaken[key]);
key = parent[key];
}
reverse(path.begin(), path.end());
return path;
}
for (int k = 0; k < 4; k++) {
Pos newPlayer = {cur.player.r + dr[k], cur.player.c + dc[k]};
if (isWall(newPlayer.r, newPlayer.c)) continue;
// push box
if (newPlayer == cur.box) {
Pos newBox = {cur.box.r + dr[k], cur.box.c + dc[k]};
if (isWall(newBox.r, newBox.c) || (isDeadCorner[newBox.r][newBox.c] && newBox != goalA) || newBox == goalB)
continue;
int ndist = boxDist(newBox);
auto key = make_pair(newPlayer, newBox);
// Chỉ cho vào nếu chưa thấy hoặc thấy nhưng hộp tiến gần hơn
if (!bestDistToGoal.count(key) || ndist < bestDistToGoal[key]) {
bestDistToGoal[key] = ndist;
pq.push({newPlayer, newBox, cur.g + 1, heuristic(newBox)});
parent[key] = {cur.player, cur.box};
moveTaken[key] = moveChar[k];
}
}
// walk
else {
auto key = make_pair(newPlayer, cur.box);
int ndist = boxDist(cur.box);
if (!bestDistToGoal.count(key) || ndist < bestDistToGoal[key]) {
bestDistToGoal[key] = ndist;
pq.push({newPlayer, cur.box, cur.g + 1, heuristic(cur.box)});
parent[key] = {cur.player, cur.box};
moveTaken[key] = moveChar[k];
}
}
}
}
return "";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
grid.resize(R);
for (int i = 0; i < R; i++) {
cin >> grid[i];
for (int j = 0; j < C; j++) {
if (grid[i][j] == 'a') myPos = {i, j};
if (grid[i][j] == 'A') goalA = {i, j};
if (grid[i][j] == 'B') goalB = {i, j};
}
}
computeDeadCorners();
vector<Pos> boxes;
for (int r = 0; r < R; r++)
for (int c = 0; c < C; c++)
if (grid[r][c] == 'X') boxes.push_back({r, c});
string bestPath;
int bestDist = 1e9;
for (auto &box : boxes) {
if (isDeadCorner[box.r][box.c] && box != goalA) continue;
int distToBox = manhattan(myPos, box);
if (distToBox > 20) continue; // filter far boxes
string path = aStarBox(myPos, box);
if (!path.empty() && distToBox < bestDist) {
bestDist = distToBox;
bestPath = path;
}
}
if (!bestPath.empty())
cout << bestPath[0] << "\n";
else {
// random move fallback
vector<int> dirs = {0, 1, 2, 3};
random_shuffle(dirs.begin(), dirs.end());
for (int k : dirs) {
int nr = myPos.r + dr[k], nc = myPos.c + dc[k];
if (!isWall(nr, nc)) {
cout << moveChar[k] << "\n";
return 0;
}
}
cout << "U\n"; // default
}
return 0;
}