Problem p01271 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2153&lang=jp
typedef long long ll;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef pair<int, int> pii;

int dlx[4] = {1, 0, -1, 0};
int dly[4] = {0, 1, 0, -1};
int drx[4] = {1, 0, -1, 0};
int dry[4] = {0, -1, 0, 1};

bool LRf[55][55][55][55];

int W, H;
bool bfs(pii l, pii r, vvc &L, vvc &R) {
  memset(LRf, false, sizeof(LRf));
  queue<pair<pii, pii>> q;
  q.push({l, r});
  while (!q.empty()) {
    auto pp = q.front();
    q.pop();
    l = pp.first;
    r = pp.second;
    if (LRf[l.first][l.second][r.first][r.second] == true)
      continue;
    LRf[l.first][l.second][r.first][r.second] = true;

    if (L[l.first][l.second] == '%' && R[r.first][r.second] == '%')
      return true;
    if (L[l.first][l.second] == '%' || R[r.first][r.second] == '%')
      continue;

    for (int i = 0; i < 4; i++) {
      int lx = l.first + dlx[i], ly = l.second + dly[i];
      int rx = r.first + drx[i], ry = r.second + dry[i];
      if (L[lx][ly] == '#' && R[rx][ry] == '#')
        continue;
      if (L[lx][ly] == '#') {
        if (LRf[l.first][l.second][rx][ry])
          continue;
        q.push({{l.first, l.second}, {rx, ry}});
      } else if (R[rx][ry] == '#') {
        if (LRf[lx][ly][r.first][r.second])
          continue;
        q.push({{lx, ly}, {r.first, r.second}});
      } else {
        if (LRf[lx][ly][rx][ry])
          continue;
        q.push({{lx, ly}, {rx, ry}});
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> W >> H) {
    if ((W | H) == 0)
      break;
    vvc L(H + 2, vc(W + 2, '#'));
    vvc R(H + 2, vc(W + 2, '#'));
    pii ls, rs;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> L[i][j];
        if (L[i][j] == 'L')
          ls = {i, j};
      }
      for (int j = 1; j <= W; j++) {
        cin >> R[i][j];
        if (R[i][j] == 'R')
          rs = {i, j};
      }
    }

    if (bfs(ls, rs, L, R)) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2153&lang=jp
typedef long long ll;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef pair<int, int> pii;

int dlx[4] = {1, 0, -1, 0};
int dly[4] = {0, 1, 0, -1};
int drx[4] = {1, 0, -1, 0};
int dry[4] = {0, -1, 0, 1};

int W, H;
bool bfs(pii l, pii r, vvi &Lf, vvi &Rf, vvc &L, vvc &R) {
  queue<pair<pii, pii>> q;
  q.push({l, r});
  while (!q.empty()) {
    auto pp = q.front();
    q.pop();
    l = pp.first;
    r = pp.second;
    Lf[l.first][l.second] = Rf[r.first][r.second] = 1;

    // cout << l.first << " " << l.second << " / " << r.first << " " << r.second
    // << endl;

    for (int i = 0; i < 4; i++) {
      int lx = l.first + dlx[i], ly = l.second + dly[i];
      int rx = r.first + drx[i], ry = r.second + dry[i];
      if (Lf[lx][ly] == 1 && Rf[rx][ry] == 1)
        continue;
      if (L[lx][ly] == '%' && R[rx][ry] == '%')
        return true;
      if (L[lx][ly] == '#' && R[rx][ry] == '#')
        continue;
      if (L[lx][ly] == '.' && R[rx][ry] == '.') {
        q.push({{lx, ly}, {rx, ry}});
      } else if (L[lx][ly] == '.' && R[rx][ry] == '#') {
        q.push({{lx, ly}, {r.first, r.second}});
      } else if (L[lx][ly] == '#' && R[rx][ry] == '.') {
        q.push({{l.first, l.second}, {rx, ry}});
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> W >> H) {
    if ((W | H) == 0)
      break;
    vvc L(H + 2, vc(W + 2, '#'));
    vvc R(H + 2, vc(W + 2, '#'));
    vvi Lf(H + 2, vi(W + 2, 0));
    vvi Rf(H + 2, vi(W + 2, 0));
    pii ls, rs;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> L[i][j];
        if (L[i][j] == 'L')
          ls = {i, j};
      }
      for (int j = 1; j <= W; j++) {
        cin >> R[i][j];
        if (R[i][j] == 'R')
          rs = {i, j};
      }
    }

    if (bfs(ls, rs, Lf, Rf, L, R)) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
}