CSES Monsters
// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#else
#define debug(x...) {};
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define double long double
#define int long long
#define MAXN 200010

const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct node {
    int x, y, d;
};
int n, m, sx, sy;
vector<vector<int>> a, dis;
vector<vector<pair<int, int>>> pre;
queue<node> q;

void bfs() {
    while (!q.empty()) {
        auto [x, y, d] = q.front(); q.pop();
        rep(i, 0, 4) {
            int nx = x + dir[i][0], ny = y + dir[i][1];
            if (a[nx][ny] && dis[nx][ny] == INF) {
                dis[nx][ny] = dis[x][y] + 1;
                q.push({nx, ny, d});
            }
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    cin >> n >> m;
    a.resize(n + 2, vector<int>(m + 2));
    dis.resize(n + 2, vector<int>(m + 2, INF));
    pre.resize(n + 2, vector<pair<int, int>>(m + 2, {-1, -1}));
    rep(i, 1, n+1) {
        string s; cin >> s;
        rep(j, 1, m+1) {
            if (s[j-1] == '.') a[i][j] = 1;
            else if (s[j-1] == 'A') sx = i, sy = j, a[i][j] = 1;
            else if (s[j-1] == 'M') {
                a[i][j] = 2;
                dis[i][j] = 0;
                q.push({i, j, 0});
            }
        }
    }
    bfs();
    q = {};
    q.push({sx, sy, 0});
    pre[sx][sy] = {-1, -1};
    vector<vector<int>> vis(n + 2, vector<int>(m + 2));
    vis[sx][sy] = 1;
    pair<int, int> found = {-1, -1};
    if (sx == 1 || sx == n || sy == 1 || sy == m) found = {sx, sy};
    while (!~found.first && !~found.second && !q.empty()) {
        auto [x, y, d] = q.front(); q.pop();
        rep(i, 0, 4) {
            int nx = x + dir[i][0], ny = y + dir[i][1];
            if (!vis[nx][ny] && a[nx][ny] && d+1 < dis[nx][ny]) {
                vis[nx][ny] = 1;
                q.push({nx, ny, d + 1});
                pre[nx][ny] = {x, y};
                if (nx == 1 || nx == n || ny == 1 || ny == m) {
                    found = {nx, ny};
                    break;
                }
            }
        }
    }
    if (!~found.first && !~found.second) {
        print("NO")
        return 0;
    }
    vector<pair<int, int>> ans;
    while (found.first != -1 && found.second != -1) {
        ans.push_back(found);
        found = pre[found.first][found.second];
    }
    reverse(ans.begin(), ans.end());
    cout << "YES\n" << sz(ans) - 1 << endl;
    rep(i, 1, sz(ans)) {
        if (ans[i].first == ans[i-1].first + 1) cout << 'D';
        else if (ans[i].first == ans[i-1].first - 1) cout << 'U';
        else if (ans[i].second == ans[i-1].second + 1) cout << 'R';
        else if (ans[i].second == ans[i-1].second - 1) cout << 'L';
    }
    cout << endl;

    return 0;
}
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next