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

#include <iostream>
#include <queue>
using namespace std;

string RenR[50], LinR[50];
bool memo[50][50][50][50];
string ans;
int h, w;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

class S {
public:
  int rx, ry, lx, ly;
  S(int lx, int ly, int rx, int ry) : lx(lx), ly(ly), rx(rx), ry(ry) {}
};

int main() {
  int ri, rj, li, lj;
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;

    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        for (int k = 0; k < 50; k++) {
          for (int l = 0; l < 50; l++)
            memo[i][j][k][l] = false;
        }
      }
    }

    for (int i = 0; i < h; i++) {
      cin >> LinR[i];
      for (int j = 0; j < w; j++) {
        if (LinR[i][j] == 'L') {
          li = i;
          lj = j;
          break;
        }
      }
      cin >> RenR[i];
      for (int j = 0; j < w; j++) {
        if (RenR[i][j] == 'R') {
          ri = i;
          rj = j;
          break;
        }
      }
    }
    queue<S> Q;
    Q.push(S(lj, li, rj, ri));
    ans = "No";
    while (!Q.empty()) {
      S s = Q.front();
      Q.pop();
      if (RenR[s.ry][s.rx] == '%' && LinR[s.ly][s.lx] == '%') {
        ans = "Yes";
        break;
      }
      if (RenR[s.ry][s.rx] == '%' || LinR[s.ly][s.lx] == '%')
        continue;
      if (memo[s.lx][s.ly][s.rx][s.ry])
        continue;
      memo[s.lx][s.ly][s.rx][s.ry] = true;

      for (int k = 0; k < 4; k++) {
        int rx = s.rx + dx[k], ry = s.ry - dy[k];
        if (rx < 0 || ry < 0 || rx >= w || ry >= h) {
          rx = s.rx;
          ry = s.ry;
        }
        if (RenR[ry][rx] == '#') {
          rx = s.rx;
          ry = s.ry;
        }
        int lx = s.lx + dx[(k + 2) % 4], ly = s.ly + dy[(k + 2) % 4];
        if (lx < 0 || ly < 0 || lx >= w || ly >= h) {
          lx = s.lx;
          ly = s.ly;
        }
        if (LinR[ly][lx] == '#') {
          lx = s.lx;
          ly = s.ly;
        }
        Q.push(S(lx, ly, rx, ry));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

string RenR[51], LinR[51];
bool memo[51][51][51][51];
string ans;
int h, w;
//         l r d u
int Rx[4] = {0, 0, 1, -1};
int Ry[4] = {1, -1, 0, 0};
//         r l d u
int Lx[4] = {0, 0, 1, -1};
int Ly[4] = {-1, 1, 0, 0};

void move(int muki, int ri, int rj, int li, int lj) {
  int ra = ri + Rx[muki], rb = rj + Ry[muki];
  if (ra < 0 || rb < 0 || ra >= h || rb >= w) {
  } else {
    if (RenR[ra][rb] != '#') {
      ri += Rx[muki];
      rj += Ry[muki];
    }
  }
  int la = li + Lx[muki], lb = lj + Ly[muki];
  if (la < 0 || lb < 0 || la >= h || lb >= w) {
  } else {
    if (LinR[la][lb] != '#') {
      li += Lx[muki];
      lj += Ly[muki];
    }
  }
  if (memo[li][lj][ri][rj])
    return;

  memo[li][lj][ri][rj] = true;

  if (RenR[ri][rj] == '%' && LinR[li][lj] == '%') {
    ans = "Yes";
    return;
  }
  if (RenR[ri][rj] == '%' || LinR[li][lj] == '%')
    return;

  for (int k = 0; k < 4; k++) {
    move(k, ri, rj, li, lj);
    if (ans == "Yes")
      break;
  }
  return;
}

int main() {
  int ri, rj, li, lj;
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < h; k++) {
          for (int l = 0; l < w; l++)
            memo[i][j][k][l] = false;
        }
      }
    }
    for (int i = 0; i < h; i++) {
      cin >> LinR[i];
      for (int j = 0; j < w; j++) {
        if (LinR[i][j] == 'L') {
          li = i;
          lj = j;
          break;
        }
      }
      cin >> RenR[i];
      for (int j = 0; j < w; j++) {
        if (RenR[i][j] == 'R') {
          ri = i;
          rj = j;
          break;
        }
      }
    }
    ans = "No";
    memo[li][lj][ri][rj] = true;
    for (int i = 0; i < 4; i++) {
      move(i, ri, rj, li, lj);
    }
    cout << ans << endl;
  }
  return 0;
}