-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabyrinth.cpp
58 lines (55 loc) · 1.4 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1e3, di[4] = {1, 0, -1, 0}, dj[4] = {0,1,0,-1};
const char dc[4] = {'D', 'R', 'U', 'L'};
int n, m, si, sj, ti, tj, d[mxN][mxN];
string s[mxN], p[mxN];
bool e(int i, int j){
return i>=0&&i<n&&j>=0&&j<m&&s[i][j]=='.';
}
int main(){
cin >> n >> m;
for(int i=0; i<n; ++i){
cin >> s[i];
for(int j=0; j<m; ++j){
if(s[i][j]=='A')
si=i, sj=j;// s[i][j] = '-';
if(s[i][j]=='B')
ti = i, tj = j, s[i][j]= '.';
}
p[i]=string(m, 0);
}
queue<ar<int, 2>> qu;
qu.push({si, sj});
while(qu.size()){
ar<int, 2> u = qu.front();
qu.pop();
s[u[0]][u[1]] = '#';
for(int k=0; k<4; ++k){
int ni = u[0]+di[k], nj= u[1]+dj[k];
if(!e(ni, nj))
continue;
qu.push({ni, nj});
s[ni][nj] = '#';
d[ni][nj] = k;
p[ni][nj] = dc[k];
}
}
if(p[ti][tj]){
cout << "YES\n";
string t;
while(ti^si || tj^sj){
t+=p[ti][tj];
int dd = d[ti][tj]^2;
ti+=di[dd];
tj+=dj[dd];
}
reverse(t.begin(), t.end());
cout << t.size() <<"\n";
cout << t;
}
else
cout << "NO";
}