Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- #include <fstream>
- #include <string>
- #include <set>
- #include <cstring>
- #include <queue>
- #define ll long long
- #define ld long double
- #define fi first
- #define se second
- #define mp make_pair
- #define pb push_back
- #define eb emplace_back
- #define all(x) (x).begin(), (x).end()
- #define INF 1e18
- #define eps 0.00001
- #define le length
- #define MOD 1000000007
- using namespace std;
- ll memo[2005][2005];
- int prices[1005];
- int pages[1005];
- int n;
- int x;
- char mat[1005][1005];
- int prev_steps[1005][1005];
- int main() {
- // ifstream cin ("input.txt");
- int n, m;
- cin >> n >> m;
- int ei, ej, si, sj;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- for(int i = 0; i < n; i++) {
- for(int j =0 ; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 'A') {
- si = i;
- sj = j;
- }
- if(mat[i][j] == 'B') {
- ei = i;
- ej = j;
- }
- }
- }
- queue<int> q;
- q.push(si);
- q.push(sj);
- q.push(0);
- vector<vector<bool> > visited(n + 1, vector<bool>(m + 1, false));
- visited[si][sj] = true;
- int path = -1;
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- int dist = q.front(); q.pop();
- if(ci == ei and cj == ej) {
- path = dist;
- break;
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != '#') {
- q.push(ti);
- q.push(tj);
- q.push(dist + 1);
- prev_steps[ti][tj] = i;
- visited[ti][tj] = true;
- }
- }
- }
- if(path == -1) {
- cout << "NO";
- return 0;
- }
- cout << "YES\n" << path << endl;
- pair<int, int> S = {si, sj}, E = {ei, ej};
- string res = "";
- while(E != S) {
- int direction = prev_steps[E.first][E.second];
- if(direction == 0) {
- res += "U";
- }
- else if(direction == 1) {
- res += "D";
- }
- else if(direction == 2) {
- res += "L";
- }
- else {
- res += "R";
- }
- E = {E.first - di[direction], E.second - dj[direction]};
- }
- reverse(res.begin(), res.end());
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement