// clang-format off #include using namespace std; using ll = long long; using ld = long double; template using V = vector; using VI = V; using VL = V; using VS = V; template using PQ = priority_queue, greater>; using G = V; template using WG = V>>; #define overload4(_1,_2,_3,_4,name,...) name #define overload3(_1,_2,_3,name,...) name #define rep1(n) for(ll _=0;_<(n);++_) #define rep2(i,n) for(ll i=0;i<(n);++i) #define rep3(i,a,b) for(ll i=(a);i<(b);++i) #define rep4(i,a,b,c) for(ll i=(a);i<(b);i+=(c)) #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__) #define rrep1(n) for(ll i=n-1;i>=0;--i) #define rrep2(i,n) for(ll i=n-1;i>=0;--i) #define rrep3(i,a,b) for(ll i=b-1;i>=a;--i) #define rrep(...) overload3(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define inside(h,w,y,x) (unsigned(y) inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } template inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template inline istream& operator>>(istream& is, V& v) { for (auto& a : v)is >> a; return is; } template inline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template inline V vec(size_t a) { return V(a); } template inline V defvec(T def, size_t a) { return V(a, def); } template inline auto vec(size_t a, Ts... ts) { return V(ts...))>(a, vec(ts...)); } template inline auto defvec(T def, size_t a, Ts... ts) { return V(def, ts...))>(a, defvec(def, ts...)); } template inline void print(const T& a) { cout << a << "\n"; } template inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } template inline void print(const V& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template inline void print(const V>& v) { for (auto& a : v)print(a); } template inline constexpr const T cum(const V& a, int l, int r) { return 0 <= l && l <= r && r < a.size() ? a[r] - (l == 0 ? 0 : a[l - 1]) : 0; }//[l,r] template inline constexpr const auto min(const T& v) { return *min_element(all(v)); } template inline constexpr const auto max(const T& v) { return *max_element(all(v)); } template inline V& operator++(V& v) { for (T& a : v)++a; return v; } template inline V& operator--(V& v) { for (T& a : v)--a; return v; } // clang-format on const int grid_size = 60; // 迷宮の大きさ const int max_hp = 1500; // 初期体力 int dy[4] = {-1, 1, 0, 0}; int dx[4] = {0, 0, -1, 1}; string dir = "UDLR"; // 探知機の情報 struct enemy { int y, x, d, num; bool destroyed; enemy(int y, int x, int d, int num) { this->y = y, this->x = x, this->d = d; this->num = num; destroyed = false; } }; // 範囲外かどうか bool range_out(int y, int x) { if (y < 0 || y >= grid_size) return true; if (x < 0 || x >= grid_size) return true; return false; } // BFSによる経路探索 string find_path(int sy, int sx, int gy, int gx, vector& S) { int siz = S.size(); vector> dist(siz, vector(siz, -1)); dist[sy][sx] = 0; queue> q; q.emplace(sy, sx); while (!q.empty()) { pair p = q.front(); q.pop(); int y = p.first, x = p.second; for (int k = 0; k < 4; k++) { int ny = y + dy[k], nx = x + dx[k]; if (range_out(ny, nx)) continue; if (dist[ny][nx] != -1) continue; char cell = S[ny][nx]; if (cell == '#' || cell == 'B' || cell == 'E') continue; dist[ny][nx] = dist[y][x] + 1; q.emplace(ny, nx); } } string res; if (dist[gy][gx] == -1) return res; int now_y = gy, now_x = gx, now_d = dist[gy][gx]; while (now_y != sy || now_x != sx) { bool moved = false; for (int k = 0; k < 4; k++) { int new_y = now_y + dy[k], new_x = now_x + dx[k]; if (range_out(new_y, new_x)) continue; if (dist[new_y][new_x] != now_d - 1) continue; now_y = new_y, now_x = new_x; now_d--; res.push_back(dir[k ^ 1]); moved = true; break; } assert(moved); } reverse(res.begin(), res.end()); return res; } int main() { init(); // 入力の受け取り int N, D, H; cin >> N >> D >> H; vector S(N); for (int i = 0; i < N; i++) cin >> S[i]; int M; cin >> M; vector E; for (int i = 0; i < M; i++) { int y, x, d; cin >> y >> x >> d; E.emplace_back(y, x, d, i); } string ans; int sy, sx, ky, kx, gy, gx; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (S[i][j] == 'S') { sy = i, sx = j; } else if (S[i][j] == 'K') { ky = i, kx = j; } else if (S[i][j] == 'G') { gy = i, gx = j; } } } // 鍵の取得 string find_key = find_path(sy, sx, ky, kx, S); ans += find_key; // 扉への移動 string goal = find_path(ky, kx, gy, gx, S); ans += goal; for (auto c : ans) { cout << "M " << c << endl; } return 0; }