-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcses1193.cpp
97 lines (91 loc) · 1.68 KB
/
cses1193.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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxN = 1e3 + 5;
char a[maxN][maxN];
int d[maxN][maxN];
bool vis[maxN][maxN];
int n, m, sx, sy, ex, ey;
int moveX[] = {0, 0, 1, -1};
int moveY[] = {1, -1, 0, 0};
char moveChar[]={'R','L','D','U'};
void bfs (int sx, int sy)
{
for (int i = 1; i <= n; i++)
{
fill_n(d[i],m+1,0);
fill_n(vis[i],m+1,false);
}
queue<pair<int,int>>q;
q.push({sx, sy});
vis[sx][sy] = true;
while(!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
if (a[x][y] == 'B') return;
for (int i = 0; i < 4; i++)
{
int u = x + moveX[i];
int v = y + moveY[i];
if (u > n || u < 1) continue;
if (v > m || v < 1) continue;
if (a[u][v] == '#') continue;
if (!vis[u][v])
{
d[u][v] = d[x][y] + 1;
vis[u][v] = true;
a[u][v]=moveChar[i];
q.push({u,v});
}
}
}
}
signed main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
if (a[i][j] == 'A')
{
sx = i;
sy = j;
}
if (a[i][j] == 'B')
{
ex = i;
ey = j;
}
}
}
bfs(sx, sy);
if (d[ex][ey] > 0) { cout << "YES\n";
cout << d[ex][ey] << '\n';
string ans = "";
int c = ex;
int currj = ey;
while(!(c == sx && currj == sy)){
ans += a[c][currj];
switch(a[c][currj]){
case 'D':
c--;
break;
case 'U':
c++;
break;
case 'R':
currj--;
break;
case 'L':
currj++;
break;
}
}
reverse(ans.begin(),ans.end());
cout <<ans;}
else cout << "NO";
}