-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabyrinth.cpp
More file actions
95 lines (73 loc) · 1.98 KB
/
Labyrinth.cpp
File metadata and controls
95 lines (73 loc) · 1.98 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
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
char ar[1001][1001];
char br[1001][1001];
bool vis[1001][1001];
int n , m;
vector<char> path;
bool isValid(int x , int y){
if(x < 1 || x > n || y < 1 || y > m) return false;
if(ar[x][y] == '#' || vis[x][y] == true) return false;
return true;
}
bool bfs(int x , int y)
{
queue<pair<int,int> > q;
q.push({x , y});
vis[x][y] = true;
while(!q.empty()){
int a = q.front().first;
int b = q.front().second;
q.pop();
if(ar[a][b] == 'B'){
while(1){
path.push_back(br[a][b]);
if(path.back() == 'L') b++;
if(path.back() == 'R') b--;
if(path.back() == 'U') a++;
if(path.back() == 'D') a--;
if(a == x && b == y)
break;
}
return true;
}
//left
if(isValid(a , b - 1)) br[a][b-1] = 'L' , q.push({a , b-1}) , vis[a][b-1] = true;
//right
if(isValid(a , b + 1)) br[a][b+1] = 'R' , q.push({a , b+1}) , vis[a][b+1] = true;
//up
if(isValid(a - 1, b)) br[a - 1][b] = 'U' , q.push({a - 1 , b}) , vis[a-1][b] = true;
//down
if(isValid(a + 1, b)) br[a + 1][b] = 'D' , q.push({a + 1 , b}) , vis[a+1][b] = true;
}
return false;
}
void solve(){
cin>>n>>m;
int x;
int y;
REP(i , n) REP(j , m){
cin>>ar[i][j];
if(ar[i][j] == 'A') x = i , y = j;
}
if(bfs(x , y) == true){
cout<<"YES"<<endl<<path.size()<<endl;
while(path.size() > 0) cout<<path.back() , path.pop_back();
}
else{
cout<<"NO";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//int t;
//cin>>t;
//while(t--)
solve();
return 0;
}