Submission details
Task:Abandoned warehouse
Sender:ind1f
Submission time:2025-09-08 16:40:07 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void bfs(int, int)':
input/code.cpp:40:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
   40 |     return 0;
      |            ^

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 5;
const int dI[4] = {-1, 0, +1, 0};
const int dJ[4] = {0, +1, 0, -1};
const string D = "URDL";

int n, m;
char a[N][N];
int sx, sy, ex, ey;
int d[N][N];
string ans;

void bfs(int x, int y) {
  memset(d, -1, sizeof(d));
  d[x][y] = 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(x, y));
  while (!q.empty()) {
    int u = q.front().first;
    int v = q.front().second;
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      int nu = u + dI[dir];
      int nv = v + dJ[dir];
      if (1 <= min(nu, nv) && nu <= n && nv <= m) {
        if (a[nu][nv] != '#') {
          if (d[nu][nv] == -1) {
            d[nu][nv] = d[u][v] + 1;
            q.push(pair<int, int>(nu, nv));
          }
        }
      }
    }
  }
  if (d[ex][ey] == -1) {
    cout << "NO" << '\n';
    return 0;
  }
  while (ex != sx || ey != sy) {
    for (int dir = 0; dir < 4; dir++) {
      int nx = ex + dI[dir];
      int ny = ey + dJ[dir];
      if (1 <= min(nx, ny) && nx <= n && ny <= m) {
        if (d[nx][ny] == d[ex][ey] - 1) {
          ans += D[dir & 1 ? 4 - dir : 2 - dir];
          ex = nx;
          ey = ny;
          break;
        }
      }
    }
  }
  reverse(ans.begin(), ans.end());
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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;
      } else if (a[i][j] == 'B') {
        ex = i, ey = j;
      }
    }
  }
  bfs(sx, sy);
  if (d[ex][ey] == -1) {
    cout << "NO" << '\n';
    return 0;
  }
  cout << "YES" << '\n';
  cout << ans.size() << '\n';
  cout << ans << '\n';
  return 0;
}