-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1193.cc
78 lines (64 loc) · 1.39 KB
/
1193.cc
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
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 1010;
bool vis[maxn][maxn];
pair<int,int> from[maxn][maxn];
char ch[maxn][maxn];
string arr[maxn];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
string s = "RDLU";
int main() {
int r, c;
cin >> r >> c;
for (int i = 0; i < r; i++)
cin >> arr[i];
int x = -1, y = -1;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
if (arr[i][j] == 'A') {
x = i;
y = j;
}
vis[i][j] = 0;
}
queue<pair<int, int>> bfs;
bfs.push({x, y});
vis[x][y] = 1;
int final_x = -1, final_y = -1;
while(!bfs.empty()) {
auto ele = bfs.front();
bfs.pop();
if (arr[ele.first][ele.second] == 'B') {
final_x = ele.first;
final_y = ele.second;
break;
}
for (int i = 0; i < 4; i++) {
int nx = ele.first + dx[i];
int ny = ele.second + dy[i];
if (!(nx >= 0 && ny >= 0 && nx < r && ny < c)) continue;
if (vis[nx][ny] || arr[nx][ny] == '#') continue;
vis[nx][ny] = 1;
from[nx][ny] = {ele.first, ele.second};
ch[nx][ny] = s[i];
bfs.push({nx, ny});
}
}
if (final_x < 0) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
string ans = "";
while(arr[final_x][final_y] != 'A') {
ans += ch[final_x][final_y];
auto it = from[final_x][final_y];
final_x = it.first;
final_y = it.second;
}
reverse(ans.begin(), ans.end());
cout << ans.size() << "\n";
cout << ans << "\n";
return 0;
}