#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;
vector<Pos> goalsA, goalsB;
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 avoidB = true; // switch: true = tránh ô 'b', false = cho phép đi vào 'b'
bool inBounds(int r, int c) {
return r >= 0 && r < R && c >= 0 && c < C;
}
bool isWall(int r, int c) {
if (!inBounds(r, c)) return true;
if (grid[r][c] == '#') return true;
if (avoidB && grid[r][c] == 'b') return true;
return false;
}
int manhattan(const Pos &a, const Pos &b) {
return abs(a.r - b.r) + abs(a.c - b.c);
}
bool isGoalA(const Pos &p) {
for (auto &g : goalsA) if (p == g) return true;
return false;
}
bool isGoalB(const Pos &p) {
for (auto &g : goalsB) if (p == g) return true;
return false;
}
int minDistGoalA(const Pos &b) {
int d = 1e9;
for (auto &g : goalsA) d = min(d, manhattan(b, g));
return d;
}
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] == '#' || isGoalA({r, c})) 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 (keeps anti-loop, limit on g, heuristic combining box->goal and player->box)
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;
unordered_map<pair<Pos, Pos>, int, HashState> bestDistToGoal;
auto heuristic = [&](const Pos &player, const Pos &box) {
return minDistGoalA(box) * 3 + manhattan(player, box);
};
auto boxDist = [&](const Pos &b) {
return minDistGoalA(b);
};
pq.push({startPlayer, startBox, 0, heuristic(startPlayer, startBox)});
bestDistToGoal[{startPlayer, startBox}] = boxDist(startBox);
while (!pq.empty()) {
State cur = pq.top(); pq.pop();
// if box already on any goalA
if (isGoalA(cur.box)) {
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;
}
// Limit search depth to avoid huge expansions
if (cur.g > 60) continue;
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 case
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] && !isGoalA(newBox)) || isGoalB(newBox))
continue;
int ndist = boxDist(newBox);
auto key = make_pair(newPlayer, newBox);
bool leavingGoal = isGoalA(cur.player) || isGoalA(newPlayer);
// anti-loop: allow push if new state unseen, or box closer,
// or if leaving a goal cell (so player can move away)
if (!bestDistToGoal.count(key) || ndist < bestDistToGoal[key] ||
(leavingGoal && ndist > 0 && ndist <= bestDistToGoal[key])) {
bestDistToGoal[key] = ndist;
pq.push({newPlayer, newBox, cur.g + 1, heuristic(newPlayer, newBox)});
parent[key] = {cur.player, cur.box};
moveTaken[key] = moveChar[k];
}
}
// walk case
else {
auto key = make_pair(newPlayer, cur.box);
int ndist = boxDist(cur.box);
bool leavingGoal = isGoalA(cur.player) || isGoalA(newPlayer);
if (!bestDistToGoal.count(key) || ndist < bestDistToGoal[key] ||
(leavingGoal && ndist > 0 && ndist <= bestDistToGoal[key])) {
bestDistToGoal[key] = ndist;
pq.push({newPlayer, cur.box, cur.g + 1, heuristic(newPlayer, 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') goalsA.push_back({i, j});
if (grid[i][j] == 'B') goalsB.push_back({i, j});
}
}
computeDeadCorners();
// gather boxes
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});
// Sort boxes by distance to player (nearest first)
sort(boxes.begin(), boxes.end(), [&](const Pos &p1, const Pos &p2) {
return manhattan(myPos, p1) < manhattan(myPos, p2);
});
string chosenPath;
// Try A* on nearest boxes in order; stop at first success
for (auto &box : boxes) {
if (isDeadCorner[box.r][box.c] && !isGoalA(box)) continue;
int distToBox = manhattan(myPos, box);
if (distToBox > 20) continue; // optional far filter
string path = aStarBox(myPos, box);
if (!path.empty()) {
chosenPath = path;
break; // stop after finding path to nearest feasible box
}
}
if (!chosenPath.empty()) {
cout << chosenPath[0] << "\n";
return 0;
}
// fallback: random legal move
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";
return 0;
}