-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabrynth.cpp
More file actions
73 lines (73 loc) · 2.16 KB
/
Copy pathLabrynth.cpp
File metadata and controls
73 lines (73 loc) · 2.16 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
#include "bits/stdc++.h"
// #include <bits/stdc++.h>
using namespace std;
struct Node {
int x, y;
};
bool isValid(int x, int y, int n, int m, vector<string>& str) {
return x >= 0 && y >= 0 && x < n && y < m && str[x][y] != '#';
}
string st;
int main() {
int n,m;
cin>>n>>m;
vector<string> str(n);
vector<vector<char> > path(n,vector<char>(m,' '));
int sti = -1, stj = -1;
for (int i = 0; i < n; i++) {
cin >> str[i];
if (sti == -1) {
for (int j = 0; j < m; j++) {
if (str[i][j] == 'A') {
sti = i;
stj = j;
break;
}
}
}
}
queue<Node> q;
Node start = {sti, stj};
q.push(start);
str[sti][stj] = '#';
int directions[4][2] = {{1, 0},{0, 1},{-1, 0},{0, -1}};
char dirChars[] = {'D', 'R', 'U', 'L'};
while (!q.empty()) {
Node node = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int newX=node.x+directions[k][0];
int newY=node.y+directions[k][1];
if (isValid(newX,newY,n,m,str)) {
path[newX][newY]=dirChars[k];
if (str[newX][newY]=='B') {
while(newX!=sti || newY!=stj){
st+=path[newX][newY];
if(path[newX][newY]=='D'){
newX--;
}
else if(path[newX][newY]=='U'){
newX++;
}
else if(path[newX][newY]=='R'){
newY--;
}
else if(path[newX][newY]=='L'){
newY++;
}
}
cout<<"YES"<<endl;
cout<<st.size()<<endl;
reverse(st.begin(),st.end());
cout<<st<<endl;
return 0;
}
str[newX][newY]='#';
Node newNode={newX,newY};
q.push(newNode);
}
}
}
cout<<"NO"<<endl;
return 0;
}