-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabyrinth.cpp
More file actions
84 lines (76 loc) · 2.08 KB
/
Labyrinth.cpp
File metadata and controls
84 lines (76 loc) · 2.08 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
//Labyrinth - https://cses.fi/problemset/task/1193
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const vector<pair<int, int>> DIRECTIONS = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
void solve() {
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
pair<int, int> source, target;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'A') source = {i, j};
if (grid[i][j] == 'B') target = {i, j};
}
}
queue<pair<int, int>> q({source});
vector<vector<int>> dist(n, vector<int>(m, -1));
vector<vector<int>> parent(n, vector<int>(m, -1));
dist[source.first][source.second] = 0;
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int dir = 0; dir < 4; dir++) {
auto [dx, dy] = DIRECTIONS[dir];
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != '#' && dist[nx][ny] == -1) {
dist[nx][ny] = 1 + dist[x][y];
parent[nx][ny] = dir;
q.push({nx, ny});
}
}
}
auto [x, y] = target;
if (dist[x][y] == -1) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
cout << dist[x][y] << endl;
string path;
while (parent[x][y] != -1) {
if (parent[x][y] == 0) {
path.push_back('R');
y--;
} else if (parent[x][y] == 1) {
path.push_back('D');
x--;
} else if (parent[x][y] == 2) {
path.push_back('U');
x++;
} else if (parent[x][y] == 3) {
path.push_back('L');
y++;
} else {
assert(false);
}
}
reverse(path.begin(), path.end());
cout << path << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}