-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabyrinth.cpp
73 lines (58 loc) · 1.85 KB
/
labyrinth.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> grid;
vector<vector<int>> dists;
vector<vector<char>> moves;
void get_dists(int i, int j) {
moves.assign(n, vector<char>(m, '-'));
dists.assign(n, vector<int>(m, -1));
dists[i][j] = 0;
queue<pair<int, int>> q;
q.emplace(i, j);
while (!q.empty()) {
i = q.front().first;
j = q.front().second;
q.pop();
for (int di = -1; di <= 1; di++)
for (int dj = -1; dj <= 1; dj++)
if ((di || dj) && !(di && dj)) {
int ni = i + di, nj = j + dj;
if (ni >= 0 && ni < n && nj >= 0 && nj < m && grid[ni][nj] != '#' && dists[ni][nj] == -1) {
q.emplace(ni, nj);
dists[ni][nj] = dists[i][j] + 1;
moves[ni][nj] = ni > i ? 'D' : (ni < i ? 'U' : (nj > j ? 'R' : 'L'));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
grid.resize(n);
int ai, aj, bi, bj;
for (int i = 0; i < n; i++) {
cin >> grid[i];
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'A') ai = i, aj = j;
else if (grid[i][j] == 'B') bi = i, bj = j;
}
}
get_dists(ai, aj);
if (dists[bi][bj] == -1) cout << "NO\n";
else {
cout << "YES\n";
cout << dists[bi][bj] << "\n";
string ans = "";
int i = bi, j = bj;
while (moves[i][j] != '-') {
ans += moves[i][j];
int ni = i + (moves[i][j] == 'D' ? -1 : (moves[i][j] == 'U' ? 1 : 0));
int nj = j + (moves[i][j] == 'R' ? -1 : (moves[i][j] == 'L' ? 1 : 0));
i = ni; j = nj;
}
reverse(ans.begin(), ans.end());
cout << ans << "\n";
}
}