forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabyrinth.cpp
125 lines (114 loc) · 3.01 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Solution by: Pranav Nair
// Problem Link: https://cses.fi/problemset/task/1193
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<vector<ll>> paths;
vector<bool> visited;
vector<ll> dist;
vector<ll> parent;
bool traversable(char c){
if((c=='.') || (c=='A') || (c=='B')) return true;
else return false;
}
void bfs(ll s){
queue<ll> q;
q.push(s);
visited[s] = true;
dist[s] = 0;
parent[s] = 0;
while(!q.empty()){
ll curr = q.front();
q.pop();
for(ll step : paths[curr]){
if(!visited[step]){
q.push(step);
visited[step] = true;
dist[step] = dist[curr] + 1;
parent[step] = curr;
}
}
}
}
int main(){
ll n=0;
ll m=0;
cin>>n>>m;
ll a=0;
ll b=0;
char room[n][m];
for(ll i=0; i<n; i++){
for(ll j=0; j<m; j++){
cin>>room[i][j];
if(room[i][j] == 'A'){
a = (m*i) + j + 1;
}
else if(room[i][j] == 'B'){
b = (m*i) + j + 1;
}
}
}
for(ll i=0; i<=(n*m); i++){
vector<ll> temp;
paths.push_back(temp);
visited.push_back(false);
dist.push_back(-1);
parent.push_back(-1);
}
for(ll i=0; i<n; i++){
for(ll j=0; j<m; j++){
ll curr = (m*i) + j + 1;
if(traversable(room[i][j])){
if((i+1)<n){
if(traversable(room[i+1][j])){
paths[curr].push_back(curr+m);
paths[curr+m].push_back(curr);
}
}
if((i-1)>=0){
if(traversable(room[i-1][j])){
paths[curr].push_back(curr-m);
paths[curr-m].push_back(curr);
}
}
if((j+1)<m){
if(traversable(room[i][j+1])){
paths[curr].push_back(curr+1);
paths[curr+1].push_back(curr);
}
}
if((j-1)>=0){
if(traversable(room[i][j-1])){
paths[curr].push_back(curr-1);
paths[curr-1].push_back(curr);
}
}
}
}
}
bfs(a);
if(dist[b] == -1){
cout<<"NO";
return 0;
}
else{
cout<<"YES\n";
vector<ll> path;
ll curr = b;
while(curr != 0){
path.push_back(curr);
curr = parent[curr];
}
reverse(path.begin(), path.end());
cout<<dist[b]<<"\n";
for(ll i=1; i<path.size(); i++){
ll curr = path[i];
ll prev = path[i-1];
if(curr == (prev+1)) cout<<"R";
else if(curr == (prev-1)) cout<<"L";
else if(curr == (prev+m)) cout<<"D";
else if(curr == (prev-m)) cout<<"U";
}
}
return 0;
}