Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxN = 1010;
- char grid[maxN][maxN];
- pair<int,int> parent[maxN][maxN];
- vector<pair<int,int>> monsters;
- int dx[8]= {1,0,-1,0,-1,-1,1,1};
- int dy[8]= {0,1,0,-1,-1,1,-1,1};
- char direction[4] = {'D','R','U','L'};
- int cost_monsters[maxN][maxN],cost_player[maxN][maxN];
- int end_x = -1,end_y = -1;
- void BFS_Monsters (int n,int m) {
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cost_monsters[i][j] = -1;
- }
- }
- queue<pair<int,int>> q;
- for (int i = 0; i < (int) monsters.size(); ++i) {
- cost_monsters[monsters[i].first][monsters[i].second] = 0;
- q.push(monsters[i]);
- }
- while (!q.empty()) {
- int x = q.front().first, y = q.front().second;
- q.pop();
- for (int i = 0; i < 4; ++i) {
- int xu = x + dx[i],xv = y + dy[i];
- if (xu >= 1 and xu <= n and xv >= 1 and xv <= m and grid[xu][xv] != '#' and cost_monsters[xu][xv] == -1) {
- cost_monsters[xu][xv] = cost_monsters[x][y] + 1;
- q.push(make_pair(xu,xv));
- }
- }
- }
- }
- bool BFS_Player(int start_x,int start_y,int n,int m) {
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cost_player[i][j] = -1;
- }
- }
- cost_player[start_x][start_y] = 0;
- queue<pair<int,int>> q;
- q.push(make_pair(start_x,start_y));
- parent[start_x][start_y] = make_pair(-1,-1);
- while (!q.empty()) {
- int x = q.front().first, y = q.front().second;
- q.pop();
- for (int i = 0; i < 4; ++i) {
- int xu = x + dx[i], xv = y + dy[i];
- if (xu >= 1 and xu <= n and xv >= 1 and xv <= m and cost_player[xu][xv] == -1 and grid[xu][xv] == '.' and (cost_player[x][y] + 1 < cost_monsters[xu][xv] or cost_monsters[xu][xv] == -1)) {
- cost_player[xu][xv] = cost_player[x][y] + 1;
- q.push(make_pair(xu,xv));
- parent[xu][xv] = make_pair(x,y);
- if (xu == 1 or xu == n or xv == 1 or xv == m) {
- end_x = xu,end_y = xv;
- return true;
- }
- }
- }
- }
- return false;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T = 1;
- //~ cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- int n,m;
- cin >> n >> m;
- int start_x = -1, start_y = -1;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cin >> grid[i][j];
- if (grid[i][j] == 'A') {
- start_x = i,start_y = j;
- }
- if (grid[i][j] == 'M') {
- monsters.push_back(make_pair(i,j));
- }
- }
- }
- BFS_Monsters(n,m);
- if (!BFS_Player(start_x,start_y,n,m)) {
- if (start_x == 1 or start_x == n or start_y == 1 or start_y == m) {
- cout << "YES\n" << 0 << '\n';
- return 0;
- }
- cout << "NO\n";
- return 0;
- }
- pair<int,int> res = make_pair(end_x,end_y);
- string ans = "";
- vector<pair<int,int>> path;
- while (res != make_pair(-1,-1)) {
- path.push_back(res);
- res = parent[res.first][res.second];
- }
- reverse(path.begin(),path.end());
- for (int i = 0; i < (int) path.size(); ++i) {
- for (int j = 0; j < 4; ++j) {
- if (path[i] == make_pair(start_x + dx[j],start_y + dy[j])) {
- ans += direction[j];
- start_x = path[i].first;
- start_y = path[i].second;
- }
- }
- }
- cout << "YES\n" << (int) ans.size() << '\n' << ans << '\n';
- }
- //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement