Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <queue>
- #include <map>
- #include <vector>
- #include <set>
- #include <cmath>
- #include <iomanip>
- #include <algorithm>
- #include <stack>
- using namespace std;
- typedef long long ll;
- typedef double db;
- vector<int> dx = { 0, 1, 0, -1 };
- vector<int> dy = { 1, 0, -1, 0 };
- vector<char> dicrets {'D', 'R', 'U', 'L'};
- struct Node
- {
- int type;
- bool was;
- bool moved;
- };
- int n, m;
- vector<vector<Node>> g;
- vector<pair<int, int>> monsters;
- bool check_is_exit(int x, int y)
- {
- return (x == 0 || y == 0 || x == m - 1 || y == n - 1);
- }
- bool check_inside(int x, int y)
- {
- return (x >= 0 && y >= 0 && x < m && y < n);
- }
- void move_monster(int direct, bool moving_new_pos)
- {
- for (int i = 0; i < monsters.size(); i++)
- {
- if (g[monsters[i].second][monsters[i].first].moved == false && moving_new_pos == false)
- {
- continue;
- }
- int new_y = monsters[i].second + dy[direct];
- int new_x = monsters[i].first + dx[direct];
- if (!check_inside(new_x, new_y))
- {
- continue;
- }
- if (g[new_y][new_x].type == 0)
- {
- if (g[new_y][new_x].moved)
- {
- continue;
- }
- }
- else if (g[new_y][new_x].type == 1)
- {
- continue;
- }
- g[new_y][new_x].moved = moving_new_pos;
- g[monsters[i].second][monsters[i].first].type = 2;
- monsters[i].first = new_x;
- monsters[i].second = new_y;
- g[monsters[i].second][monsters[i].first].type = 0;
- }
- }
- vector<int> res;
- bool dfs(int x, int y)
- {
- if (g[y][x].was)
- {
- return false;
- }
- g[y][x].was = true;
- if (check_is_exit(x, y))
- {
- return true;
- }
- for (int i = 0; i < 4; i++)
- {
- //move all monsters
- move_monster(i, true);
- if (g[y + dy[i]][x + dx[i]].type == 2)
- {
- if (dfs(x + dx[i], y + dy[i]))
- {
- res.push_back(i);
- return true;
- }
- }
- // move back
- move_monster((i + 2) % 4, false);
- }
- return false;
- }
- int main()
- {
- cin >> n >> m;
- g.resize(n, vector<Node> (m));
- int start_x = 0;
- int start_y = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- char c;
- cin >> c;
- if (c == 'A')
- {
- start_y = i;
- start_x = j;
- g[i][j].type = 2;
- }
- else if (c == 'M')
- {
- g[i][j].type = 0;
- monsters.push_back(make_pair(j, i));
- }
- else
- {
- g[i][j].type = ((c == '#') ? 1 : 2);
- }
- }
- }
- if (dfs(start_x, start_y))
- {
- cout << "YES" << endl << res.size() << endl;
- for (int i = res.size() - 1; i >= 0; i--)
- {
- cout << dicrets[res[i]];
- }
- }
- else
- {
- cout << "NO";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement