#include <iostream> #include <algorithm> #include <queue> #include <array> #include <numeric> #include <vector> struct DSU { std::vector<int> p; std::vector<int> r; DSU(int n) : p(n), r(n, 0) { std::iota(p.begin(), p.end(), 0); } int find(int i) { return i == p[i] ? i : p[i] = find(p[i]); } void merge(int i, int j) { i = find(i); j = find(j); if (i == j) return; if (r[i] < r[j]) std::swap(i, j); if (r[i] == r[j]) r[i] += 1; p[j] = i; } }; enum { U, D, L, R, }; int dir_x[] = {0, 0, -1, 1}; int dir_y[] = {-1, 1, 0, 0}; int main() { size_t h, w; std::cin >> h >> w; int x, y; std::vector<std::string> b(h); for (size_t i = 0; i < h; ++i) { std::cin >> b[i]; for (size_t j = 0; j < w; ++j) { if (b[i][j] == 'S') { x = i; y = j; b[i][j] = '.'; } } } //std::cerr << "XY\t" << x << ' ' << y << '\n'; int n = (h + 1) * w + h * (w + 1); DSU dsu(n); auto idx_u = [&](int i, int j) { return i + (h + 1) * j; }; auto idx_d = [&](int i, int j) { return i + (h + 1) * j + 1; }; auto idx_l = [&](int i, int j) { return j + (w + 1) * i + w * (h + 1); }; auto idx_r = [&](int i, int j) { return j + (w + 1) * i + 1 + w * (h + 1); }; auto idx = [&](int i, int j, int d) { if (d == U) return idx_u(i, j); if (d == D) return idx_d(i, j); if (d == L) return idx_l(i, j); if (d == R) return idx_r(i, j); }; std::vector<std::array<int, 2>> edges; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (b[i][j] == '.') { dsu.merge(idx_l(i, j), idx_r(i, j)); dsu.merge(idx_u(i, j), idx_d(i, j)); } if (b[i][j] == '/') { dsu.merge(idx_l(i, j), idx_u(i, j)); dsu.merge(idx_r(i, j), idx_d(i, j)); edges.push_back({idx_l(i, j), idx_r(i, j)}); } if (b[i][j] == '\\') { dsu.merge(idx_l(i, j), idx_d(i, j)); dsu.merge(idx_r(i, j), idx_u(i, j)); edges.push_back({idx_l(i, j), idx_r(i, j)}); } } } std::vector<std::vector<int>> g(n); for (auto &e : edges) { e[0] = dsu.find(e[0]); e[1] = dsu.find(e[1]); g[e[0]].push_back(e[1]); g[e[1]].push_back(e[0]); } std::vector<int> dist(n, 1<<30); std::queue<int> queue; dist[dsu.find(idx_u(x, y))] = 0; dist[dsu.find(idx_l(x, y))] = 0; queue.push(dsu.find(idx_u(x, y))); queue.push(dsu.find(idx_l(x, y))); while (queue.size() > 0) { auto v = queue.front(); queue.pop(); for (auto u : g[v]) { if (dist[u] > dist[v] + 1) { dist[u] = dist[v] + 1; queue.push(u); } } } //for (int i = 0; i < h; ++i) { //for (int j = 0; j < w; ++j) { //std::cerr << "D\t" << i << ' ' << j << ' ' << dist[dsu.find(idx_l(i, j))] << '\n'; //} //} int p = 0; int q = 0; int d = D; bool good = false; for (int i = 0; i < h; ++i) { auto a = dsu.find(idx(p, q, d ^ 1)); auto b = dsu.find(idx(i, 0, L)); if (dist[b] < dist[a]) { //std::cerr << i << ' ' << 0 << " R " << dist[b] << '\n'; p = i; q = 0; d = R; good = true; } auto c = dsu.find(idx(i, w - 1, R)); if (dist[c] < std::min(dist[a], dist[b])) { //std::cerr << i << ' ' << w - 1 << " L " << dist[c] << '\n'; p = i; q = w - 1; d = L; good = true; } } for (int j = 0; j < w; ++j) { auto a = dsu.find(idx(p, q, d ^ 1)); auto b = dsu.find(idx(0, j, U)); if (dist[b] < dist[a]) { //std::cerr << 0 << ' ' << j << " D " << dist[b] << '\n'; p = 0; q = j; d = D; good = true; } auto c = dsu.find(idx(h - 1, j, D)); if (dist[c] < std::min(dist[a], dist[b])) { //std::cerr << h - 1 << ' ' << j << " U " << dist[c] << '\n'; p = h - 1; q = j; d = U; good = true; } } if (!good) { std::cout << "NO\n"; return 0; } //std::cerr << "DIST\t" << dist[dsu.find(idx(p, q, d ^ 1))] << '\n'; int time = 0; std::vector<std::array<int, 3>> out; for (; p != x || q != y; time += 1) { //std::cerr << p << ' ' << q << ' ' << "UDLR"[d] << ' ' << b[p][q] << ' ' << dist[dsu.find(idx(p, q, d ^ 1))] << ' ' << dist[dsu.find(idx(p, q, d))] << '\n'; if (b[p][q] == '.') { if (b[p + dir_y[d]][q + dir_x[d]] == '#') { d ^= 1; } else { p += dir_y[d]; q += dir_x[d]; } continue; } if (dist[dsu.find(idx(p, q, d))] < dist[dsu.find(idx(p, q, d ^ 1))]) { //std::cerr << "BREAK\n"; out.push_back({time, p, q}); } else { d ^= 2; if (b[p][q] == '/') { d ^= 1; } else { } } p += dir_y[d]; q += dir_x[d]; //std::cerr << "L\t" << p << ' ' << q << ' ' << "UDLR"[d] << '\n'; } std::reverse(out.begin(), out.end()); std::cout << "YES\n"; std::cout << "UDLR"[d ^ 1] << '\n'; std::cout << out.size() << '\n'; for (auto [t, r, c] : out) { std::cout << time - t << ' ' << r + 1 << ' ' << c + 1 << '\n'; } }