-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathLabyrinth.cpp
66 lines (58 loc) · 1.72 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define x first
#define y second
const int h[] = {1, -1, 0, 0}, v[] = {0, 0, 1, -1};
bool vis[1000][1000];
char c, par[1000][1000], ans[1000000];
int N, M, sx, sy, ex, ey, dist[1000][1000];
queue<pii> Q;
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
scanf(" %c", &c);
if(c == '#') vis[i][j] = true;
else if(c == 'A'){
sx = i; sy = j;
} else if(c == 'B'){
ex = i; ey = j;
}
}
}
vis[sx][sy] = true;
Q.push({sx, sy});
while(!Q.empty()){
pii P = Q.front(); Q.pop();
for(int i = 0; i < 4; i++){
int dx = P.x+h[i];
int dy = P.y+v[i];
if(0 <= dx && dx < N && 0 <= dy && dy < M && !vis[dx][dy]){
if(i == 0) par[dx][dy] = 'D';
else if(i == 1) par[dx][dy] = 'U';
else if(i == 2) par[dx][dy] = 'R';
else if(i == 3) par[dx][dy] = 'L';
dist[dx][dy] = dist[P.x][P.y]+1;
vis[dx][dy] = true;
Q.push({dx, dy});
}
}
}
if(!vis[ex][ey]){
printf("NO\n");
return 0;
}
printf("YES\n%d\n", dist[ex][ey]);
pii P = {ex, ey};
for(int i = dist[ex][ey]; i > 0; i--){
ans[i] = par[P.x][P.y];
if(ans[i] == 'D') P = {P.x-1, P.y};
else if(ans[i] == 'U') P = {P.x+1, P.y};
else if(ans[i] == 'R') P = {P.x, P.y-1};
else if(ans[i] == 'L') P = {P.x, P.y+1};
}
for(int i = 1; i <= dist[ex][ey]; i++)
printf("%c", ans[i]);
printf("\n");
}