-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonsters.cpp
More file actions
85 lines (80 loc) · 2.1 KB
/
monsters.cpp
File metadata and controls
85 lines (80 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
struct s{
int x, y;
unsigned int d;
};
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
string rev = "LRDU";
int N, M;
string g[1000];
unsigned int md[1000][1000], di[1000][1000];
int32_t main(){
cin >> N >> M;
for(int i = 0; i < N; i++){
cin >> g[i];
}
queue<s> q;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(g[i][j] == 'M'){
q.push({i, j, 0});
}
}
}
fill(md[0], md[N-1]+M, -1);
while(!q.empty()){
s cur = q.front();
q.pop();
int x = cur.x, y = cur.y;
unsigned int d = cur.d;
if(x < 0 || y < 0 || x >= N || y >= M || g[x][y] == '#' || ~md[x][y]) continue;
md[x][y] = d;
for(int i = 0; i < 4; i++){
int nx = x+dx[i], ny = y+dy[i];
q.push({nx, ny, d+1});
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(g[i][j] == 'A'){
q.push({i, j, 0});
}
}
}
fill(di[0], di[N-1]+M, -1);
while(!q.empty()){
s cur = q.front();
q.pop();
int x = cur.x, y = cur.y;
unsigned int d = cur.d;
if(g[x][y] == '#' || ~di[x][y] || d >= md[x][y]) continue;
di[x][y] = d;
if(x == 0 || y == 0 || x == N-1 || y == M-1){
cout << "YES" << endl << d << endl;
stack<char> ans;
while(g[x][y] != 'A'){
for(int i = 0; i < 4; i++){
int nx = x+dx[i], ny = y+dy[i];
if(di[nx][ny] == di[x][y]-1){
ans.push(rev[i]);
x = nx, y = ny;
break;
}
}
}
while(!ans.empty()){
cout << ans.top();
ans.pop();
}
cout << endl;
return 0;
}
for(int i = 0; i < 4; i++){
int nx = x+dx[i], ny = y+dy[i];
q.push({nx, ny, d+1});
}
}
cout << "NO" << endl;
}