Problem p01271 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
char a[52][52], b[52][52];
set<tuple<int, int, int, int>> dp;
int ans;

void calc(int lx, int ly, int rx, int ry) {
  if (ans == 1)
    return;
  if (a[lx][ly] == '%' && b[rx][ry] == '%') {
    ans = 1;
    return;
  } else if (a[lx][ly] == '%' || b[rx][ry] == '%')
    return;
  tuple<int, int, int, int> now;
  now = make_tuple(lx, ly, rx, ry);
  decltype(dp)::iterator it = dp.find(now);
  if (it != dp.end())
    return;
  dp.insert(now);
  int nxl = lx, nyl = ly, nxr = rx, nyr = ry;
  if (a[lx + 1][ly] != '#')
    nxl++;
  if (b[rx - 1][ry] != '#')
    nxr--;
  calc(nxl, nyl, nxr, nyr);
  nxl = lx;
  nyl = ly;
  nxr = rx;
  nyr = ry;
  if (a[lx - 1][ly] != '#')
    nxl--;
  if (b[rx + 1][ry] != '#')
    nxr++;
  calc(nxl, nyl, nxr, nyr);
  nxl = lx;
  nyl = ly;
  nxr = rx;
  nyr = ry;
  if (a[lx][ly + 1] != '#')
    nyl++;
  if (b[rx][ry + 1] != '#')
    nyr++;
  calc(nxl, nyl, nxr, nyr);
  nxl = lx;
  nyl = ly;
  nxr = rx;
  nyr = ry;
  if (a[lx][ly - 1] != '#')
    nyl--;
  if (b[rx][ry - 1] != '#')
    nyr--;
  calc(nxl, nyl, nxr, nyr);
  return;
}

int main() {
  while (1) {
    int w, h;
    cin >> w >> h;
    int stxl, styl, stxr, styr;
    if (w == 0 && h == 0)
      break;
    lp(i, 52) {
      lp(j, 52) {
        a[i][j] = '#';
        b[i][j] = '#';
      }
    }
    lp(i, h + 1) {
      if (i == 0)
        continue;
      lp(j, w + 1) {
        if (j == 0)
          continue;
        cin >> a[j][i];
        if (a[j][i] == 'L') {
          stxl = j;
          styl = i;
        }
      }
      lp(j, w + 1) {
        if (j == 0)
          continue;
        cin >> b[j][i];
        if (b[j][i] == 'R') {
          stxr = j;
          styr = i;
        }
      }
    }
    dp.clear();
    ans = 0;
    calc(stxl, styl, stxr, styr);
    if (ans == 0)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
char a[52][52], b[52][52];
set<tuple<int, int, int, int>> dp;
int ans;

void calc(int lx, int ly, int rx, int ry) {
  queue<tuple<int, int, int, int>> Q;
  Q.push(make_tuple(lx, ly, rx, ry));
  while (!Q.empty()) {
    lx = get<0>(Q.front());
    ly = get<1>(Q.front());
    rx = get<2>(Q.front());
    ry = get<3>(Q.front());
    Q.pop();
    if (a[lx][ly] == '%' && b[rx][ry] == '%') {
      ans = 1;
      return;
    } else if (a[lx][ly] == '%' || b[rx][ry] == '%')
      continue;
    tuple<int, int, int, int> now;
    now = make_tuple(lx, ly, rx, ry);
    decltype(dp)::iterator it = dp.find(now);
    if (it != dp.end())
      continue;
    dp.insert(now);
    int nxl = lx, nyl = ly, nxr = rx, nyr = ry;
    if (a[lx + 1][ly] != '#')
      nxl++;
    if (b[rx - 1][ry] != '#')
      nxr--;
    Q.push(make_tuple(nxl, nyl, nxr, nyr));
    nxl = lx;
    nyl = ly;
    nxr = rx;
    nyr = ry;
    if (a[lx - 1][ly] != '#')
      nxl--;
    if (b[rx + 1][ry] != '#')
      nxr++;
    Q.push(make_tuple(nxl, nyl, nxr, nyr));
    nxl = lx;
    nyl = ly;
    nxr = rx;
    nyr = ry;
    if (a[lx][ly + 1] != '#')
      nyl++;
    if (b[rx][ry + 1] != '#')
      nyr++;
    Q.push(make_tuple(nxl, nyl, nxr, nyr));
    nxl = lx;
    nyl = ly;
    nxr = rx;
    nyr = ry;
    if (a[lx][ly - 1] != '#')
      nyl--;
    if (b[rx][ry - 1] != '#')
      nyr--;
    Q.push(make_tuple(nxl, nyl, nxr, nyr));
  }
  return;
}

int main() {
  while (1) {
    int w, h;
    cin >> w >> h;
    int stxl, styl, stxr, styr;
    if (w == 0 && h == 0)
      break;
    lp(i, 52) {
      lp(j, 52) {
        a[i][j] = '#';
        b[i][j] = '#';
      }
    }
    lp(i, h + 1) {
      if (i == 0)
        continue;
      lp(j, w + 1) {
        if (j == 0)
          continue;
        cin >> a[j][i];
        if (a[j][i] == 'L') {
          stxl = j;
          styl = i;
        }
      }
      lp(j, w + 1) {
        if (j == 0)
          continue;
        cin >> b[j][i];
        if (b[j][i] == 'R') {
          stxr = j;
          styr = i;
        }
      }
    }
    dp.clear();
    ans = 0;
    calc(stxl, styl, stxr, styr);
    if (ans == 0)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}