-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab.cpp
81 lines (64 loc) · 2.09 KB
/
lab.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
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<bool>;
ll n, m;
int main() {
cin >> n >> m;
vector<vi> a(n, vi(m, 0));
pi start;
pi end;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == '#') a[i][j] = true;
if (c == 'A') start = {i, j};
if (c == 'B') end = {i, j};
}
}
// bfs
queue<pi> q;
vector<vector<int>> dist(n, vector<int>(m, INT_MAX));
vector<vector<pi>> parent(n, vector<pi>(m, {-1, -1}));
dist[start.first][start.second] = 0;
q.push(start);
vector<pi> adj = {
{1, 0},
{-1, 0},
{0, 1},
{0, -1}};
while (!q.empty()) {
pi top = q.front();
q.pop();
if (top == end) {
cout << "YES" << endl;
cout << dist[top.first][top.second] << endl;
vector<char> seq;
for (pi curr = top; curr != start; curr = parent[curr.first][curr.second]) {
int ud_diff = curr.first - parent[curr.first][curr.second].first;
int lr_diff = curr.second - parent[curr.first][curr.second].second;
if (ud_diff > 0) seq.push_back('D');
if (ud_diff < 0) seq.push_back('U');
if (lr_diff > 0) seq.push_back('R');
if (lr_diff < 0) seq.push_back('L');
}
reverse(seq.begin(), seq.end());
for (char c : seq) cout << c;
cout << endl;
return 0;
}
for (pi dir : adj) {
pi loc = {dir.first + top.first, dir.second + top.second};
if (loc.first < 0 || loc.first >= n || loc.second < 0 || loc.second >= m) continue;
if (dist[loc.first][loc.second] == INT_MAX && !a[loc.first][loc.second]) {
parent[loc.first][loc.second] = top;
dist[loc.first][loc.second] = dist[top.first][top.second] + 1;
q.push(loc);
}
}
}
cout << "NO" << endl;
return 0;
}