Problem p01271 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct P {
  int rx, ry, lx, ly;
  P() {}
  P(int rx, int ry, int lx, int ly) : rx(rx), ry(ry), lx(lx), ly(ly){};
};
int w, h, dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
bool used[50][50][50][50];
char Rin[50][50], Len[50][50];
bool bfs(int, int, int, int);
int main() {
  int rx, ry, lx, ly;
  while (cin >> w >> h && w || h) {
    memset(used, false, sizeof(used));
    for (int i = 0; i < h; i++) {
      cin >> Len[i] >> Rin[i];
      for (int j = 0; j < w; j++) {
        if (Rin[i][j] == 'R')
          rx = i, ry = j;
        if (Len[i][j] == 'L')
          lx = i, ly = j;
      }
    }
    cout << (bfs(rx, ry, lx, ly) ? "Yes" : "No") << endl;
  }
}
bool bfs(int rx, int ry, int lx, int ly) {
  queue<P> que;
  que.push(P(rx, ry, lx, ly));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (Rin[p.rx][p.ry] == '%' && Len[p.lx][p.ly] == '%')
      return true;
    if (Rin[p.rx][p.ry] == '%' || Len[p.lx][p.ly] == '%')
      continue;
    used[p.rx][p.ry][p.lx][p.ly] = true;
    for (int i = 0; i < 4; i++) {
      int nrx = p.rx + dx[i], nry = p.ry + dy[i];
      int nlx = p.lx + dx[i], nly = p.ly - dy[i];
      if (nrx < 0 || nry < 0 || nrx >= h || nry >= w || Rin[nrx][nry] == '#')
        nrx = p.rx, nry = p.ry;
      if (nlx < 0 || nly < 0 || nlx >= h || nly >= w || Len[nlx][nly] == '#')
        nlx = p.lx, nly = p.ly;
      if (used[nrx][nry][nlx][nly])
        continue;
      que.push(P(nrx, nry, nlx, nly));
    }
  }
  return false;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct P {
  int rx, ry, lx, ly;
  P() {}
  P(int rx, int ry, int lx, int ly) : rx(rx), ry(ry), lx(lx), ly(ly){};
};
int w, h, dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
bool used[50][50][50][50];
char Rin[50][50], Len[50][50];
bool bfs(int, int, int, int);
int main() {
  int rx, ry, lx, ly;
  while (cin >> w >> h && w || h) {
    memset(used, false, sizeof(used));
    for (int i = 0; i < h; i++) {
      cin >> Len[i] >> Rin[i];
      for (int j = 0; j < w; j++) {
        if (Rin[i][j] == 'R')
          rx = i, ry = j;
        if (Len[i][j] == 'L')
          lx = i, ly = j;
      }
    }
    cout << (bfs(rx, ry, lx, ly) ? "Yes" : "No") << endl;
  }
}
bool bfs(int rx, int ry, int lx, int ly) {
  queue<P> que;
  que.push(P(rx, ry, lx, ly));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (used[p.rx][p.ry][p.lx][p.ly])
      continue;
    if (Rin[p.rx][p.ry] == '%' && Len[p.lx][p.ly] == '%')
      return true;
    if (Rin[p.rx][p.ry] == '%' || Len[p.lx][p.ly] == '%')
      continue;
    used[p.rx][p.ry][p.lx][p.ly] = true;
    for (int i = 0; i < 4; i++) {
      int nrx = p.rx + dx[i], nry = p.ry + dy[i];
      int nlx = p.lx + dx[i], nly = p.ly - dy[i];
      if (nrx < 0 || nry < 0 || nrx >= h || nry >= w || Rin[nrx][nry] == '#')
        nrx = p.rx, nry = p.ry;
      if (nlx < 0 || nly < 0 || nlx >= h || nly >= w || Len[nlx][nly] == '#')
        nlx = p.lx, nly = p.ly;
      if (used[nrx][nry][nlx][nly])
        continue;
      que.push(P(nrx, nry, nlx, nly));
    }
  }
  return false;
}