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

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define F first
#define S second
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;

int mcrdx[] = {1, 0, -1, 0};
int mcldx[] = {-1, 0, 1, 0};
int mcdy[] = {0, 1, 0, -1};
char fL[51][51], fR[51][51];
int mcH, mcW;
bool mcok[51][51][51][51];
string mcdfs(pair<int, int> sr, pair<int, int> sl) {
  stack<pair<pair<int, int>, pair<int, int>>> st;
  st.push({sr, sl});
  while (!st.empty()) {
    pair<int, int> r = st.top().F, l = st.top().S;
    st.pop();
    if (fR[r.S][r.F] == '%' && fL[l.S][l.F] == '%') {
      return "Yes";
    } else if (fR[r.S][r.F] == '%' || fL[l.S][l.F] == '%') {
      continue;
    }
    REP(i, 0, 4) {
      pair<int, int> nr = {r.F + mcrdx[i], r.S + mcdy[i]},
                     nl = {l.F + mcldx[i], l.S + mcdy[i]};
      if (!ck(nr.F, mcW) || !ck(nr.S, mcH))
        nr = r;
      else if (fR[nr.S][nr.F] == '#')
        nr = r;
      if (!ck(nl.F, mcW) || !ck(nl.S, mcH))
        nl = l;
      else if (fL[nl.S][nl.F] == '#')
        nl = l;

      //	cout<<nr.F<<" "<<nr.S<<" "<<nl.F<<" "<<nl.S<<" "<<endl;
      if (!mcok[nr.F][nr.S][nl.F][nl.S]) {
        mcok[nr.F][nr.S][nl.F][nl.S] = true;
        st.push({nr, nl});
      }
    }
  }
  return "No";
}
int main() {

  while (cin >> mcW >> mcH, mcW) {
    pair<int, int> Rin, Len;
    memset(mcok, false, sizeof(mcok));
    REP(i, 0, mcH) {
      REP(j, 0, mcW) {
        cin >> fL[i][j];
        if (fL[i][j] == 'L')
          Len = {j, i};
      }
      REP(j, 0, mcW) {
        cin >> fR[i][j];
        if (fR[i][j] == 'R')
          Rin = {j, i};
      }
    }

    p(mcdfs(Rin, Len));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define F first
#define S second
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;

int mcrdx[] = {1, 0, -1, 0};
int mcldx[] = {-1, 0, 1, 0};
int mcdy[] = {0, 1, 0, -1};
char fL[51][51], fR[51][51];
int mcH, mcW;
bool mcok[51][51][51][51];
bool goal;
void mcdfs(pair<int, int> r, pair<int, int> l) {
  if (mcok[r.F][r.S][l.F][l.S])
    return;
  mcok[r.F][r.S][l.F][l.S] = true;
  if (fR[r.S][r.F] == '%' && fL[l.S][l.F] == '%') {
    goal = true;
    memset(mcok, true, sizeof(mcok));
    return;
  } else if (fR[r.S][r.F] == '%' || fL[l.S][l.F] == '%') {
    return;
  }
  REP(i, 0, 4) {
    pair<int, int> nr = {r.F + mcrdx[i], r.S + mcdy[i]},
                   nl = {l.F + mcldx[i], l.S + mcdy[i]};
    bool okr = true, okl = true;
    if (!ck(nr.F, mcW) || !ck(nr.S, mcH))
      okr = false;
    else if (fR[nr.S][nr.F] == '#')
      okr = false;
    if (!ck(nl.F, mcW) || !ck(nl.S, mcH))
      okl = false;
    else if (fL[nl.S][nl.F] == '#')
      okl = false;

    if (!okr && !okl)
      continue;
    // cout<<nr.F<<" "<<nr.S<<" "<<nl.F<<" "<<nl.S<<" "<<endl;
    if (okr && !okl)
      mcdfs(nr, l);
    else if (!okr && okl)
      mcdfs(r, nl);
    else if (okr && okl)
      mcdfs(nr, nl);
  }

  return;
}
int main() {

  while (cin >> mcW >> mcH, mcW) {
    pair<int, int> Rin, Len;
    memset(mcok, false, sizeof(mcok));
    REP(i, 0, mcH) {
      REP(j, 0, mcW) {
        cin >> fL[i][j];
        if (fL[i][j] == 'L')
          Len = {j, i};
      }
      REP(j, 0, mcW) {
        cin >> fR[i][j];
        if (fR[i][j] == 'R')
          Rin = {j, i};
      }
    }
    goal = false;
    mcdfs(Rin, Len);
    p(goal ? "Yes" : "No");
  }
  return 0;
}