#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 {
// simple custom hash
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;
// Directions
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;
}
// Hard wall: chỉ tường thật sự '#' hoặc ngoài biên
bool isHardWall(int r, int c) {
return !inBounds(r, c) || grid[r][c] == '#';
}
// Walk-block: dùng cho bước đi bình thường (bao gồm 'b' là chặn)
bool isWalkBlocked(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);
}
// Dead corner precompute: nếu 1 ô trống kề 2 bức tường theo góc vuông (không tính goalA)
void computeDeadCorners() {
isDeadCorner.assign(R, vector<bool>(C, false));
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
if (!inBounds(r,c)) continue;
if (grid[r][c] == '#') continue;
if (Pos{r,c} == goalA) continue;
bool up = isHardWall(r - 1, c), down = isHardWall(r + 1, c);
bool left = isHardWall(r, c - 1), right = isHardWall(r, c + 1);
if ((up && left) || (up && right) || (down && left) || (down && right)) {
isDeadCorner[r][c] = true;
}
}
}
}
/*
####
.X..
.aX.
....
*/
// A* search: returns path string (sequence of 'U','D','L','R') or "" if impossible
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 &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();
// goal: box on goalA
if (cur.box == goalA) {
// reconstruct path
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]};
// --- walk case (bình thường) ---
if (!(newPlayer == cur.box)) {
// If walk blocked (includes 'b'), skip
if (isWalkBlocked(newPlayer.r, newPlayer.c)) continue;
pair<Pos,Pos> nxtKey = {newPlayer, cur.box};
int ndist = boxDist(cur.box);
if (!bestDistToGoal.count(nxtKey) || ndist < bestDistToGoal[nxtKey]) {
bestDistToGoal[nxtKey] = ndist;
parent[nxtKey] = {cur.player, cur.box};
moveTaken[nxtKey] = moveChar[k];
pq.push({newPlayer, cur.box, cur.g + 1, heuristic(cur.box)});
}
}
// --- push case ---
else {
Pos newBox = {cur.box.r + dr[k], cur.box.c + dc[k]};
// 1) newBox must be in bounds and not a hard wall ('#')
if (!inBounds(newBox.r, newBox.c)) continue;
if (isHardWall(newBox.r, newBox.c)) continue;
// 2) disallow pushing into 'b' (other actor) — treat as blocked for push
if (grid[newBox.r][newBox.c] == 'b') continue;
// 3) disallow pushing into another box (unless that cell is goalA and you allow stacking)
if (grid[newBox.r][newBox.c] == 'X' && !(newBox == goalA)) continue;
// 4) disallow pushing into dead corner (unless it's goalA)
if (isDeadCorner[newBox.r][newBox.c] && !(newBox == goalA)) continue;
// 5) disallow pushing into opponent goal B
if (newBox == goalB) continue;
// pass checks -> consider new state
pair<Pos,Pos> nxtKey = {newPlayer, newBox};
int ndist = boxDist(newBox);
if (!bestDistToGoal.count(nxtKey) || ndist < bestDistToGoal[nxtKey]) {
bestDistToGoal[nxtKey] = ndist;
parent[nxtKey] = {cur.player, cur.box};
moveTaken[nxtKey] = moveChar[k];
pq.push({newPlayer, newBox, cur.g + 1, heuristic(newBox)});
}
}
}
}
return "";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// Read grid (R x C assumed 16x16 by default)
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();
// 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});
// Greedy: choose nearest box (by manhattan to player) that A* can plan for
string bestPath;
int bestDist = INT_MAX;
for (auto &box : boxes) {
// skip if box already in dead corner (and not goal)
if (isDeadCorner[box.r][box.c] && box != goalA) continue;
int distToBox = manhattan(myPos, box);
if (distToBox > 50) continue; // optional prune for very far boxes
string path = aStarBox(myPos, box);
if (!path.empty() && distToBox < bestDist) {
bestDist = distToBox;
bestPath = path;
}
}
if (!bestPath.empty()) {
cout << bestPath[0] << "\n";
return 0;
}
// fallback: random legal move (respecting isWalkBlocked)
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 (!inBounds(nr,nc)) continue;
if (isWalkBlocked(nr,nc)) continue;
if (grid[nr][nc] == 'X') continue; // don't step onto other box
cout << moveChar[k] << "\n";
return 0;
}
cout << "S\n";
return 0;
}