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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

const int kMax_N = 51;
char fieldL[kMax_N + 1][kMax_N + 1];
char fieldR[kMax_N + 1][kMax_N + 1];
bool checked[kMax_N + 1][kMax_N + 1][kMax_N + 1][kMax_N + 1];

const int dyL[] = {-1, 0, 1, 0};
const int dxL[] = {0, 1, 0, -1};
const int dyR[] = {-1, 0, 1, 0};
const int dxR[] = {0, -1, 0, 1};

int w, h;

struct Search {
  int ly, lx;
  int ry, rx;
  Search() = default;
  Search(int a, int b, int c, int d) : ly(a), lx(b), ry(c), rx(d) {}
};
int main() {
  while (true) {
    cin >> w >> h;
    if (w + h == 0) {
      break;
    }
    int ly, lx, ry, rx;
    for (int i = 0; i < h; i++) {
      string strL, strR;
      cin >> strL >> strR;
      for (int j = 0; j < w; j++) {
        fieldL[i][j] = strL[j];
        fieldR[i][j] = strR[j];
        if (strL[j] == 'L') {
          ly = i;
          lx = j;
        }
        if (strR[j] == 'R') {
          ry = i;
          rx = j;
        }
      }
    }

    memset(checked, false, sizeof(checked));

    queue<Search> que;
    que.emplace(ly, lx, ry, rx);
    bool ok = false;
    while (!que.empty()) {
      Search s = que.front();
      que.pop();
      if (fieldL[s.ly][s.lx] == fieldR[s.ry][s.rx] &&
          fieldL[s.ly][s.lx] == '%') {
        ok = true;
        break;
      }
      if (fieldL[s.ly][s.lx] == '%') {
        continue;
      }
      if (fieldR[s.ry][s.rx] == '%') {
        continue;
      }
      if (checked[s.ly][s.lx][s.ry][s.rx]) {
        continue;
      }
      checked[s.ly][s.lx][s.ry][s.rx] = true;
      for (int i = 0; i < 4; i++) {
        int nly = s.ly + dyL[i];
        int nlx = s.lx + dxL[i];
        int nry = s.ry + dyR[i];
        int nrx = s.rx + dxR[i];
        if (nly < 0)
          nly = 0;
        if (nly >= h)
          nly = h - 1;
        if (nlx < 0)
          nlx = 0;
        if (nlx >= w)
          nlx = w - 1;
        if (nry < 0)
          nry = 0;
        if (nry >= h)
          nry = h - 1;
        if (nrx < 0)
          nrx = 0;
        if (nrx >= w)
          nrx = w - 1;
        if (fieldL[nly][nlx] == '#') {
          nly = s.ly;
          nlx = s.lx;
        }
        if (fieldR[nry][nrx] == '#') {
          nry = s.ry;
          nrx = s.rx;
        }
        que.emplace(nly, nlx, nry, nrx);
      }
    }
    if (ok) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const int kMax_N = 51;
char fieldL[kMax_N + 1][kMax_N + 1];
char fieldR[kMax_N + 1][kMax_N + 1];
bool checked[kMax_N + 1][kMax_N + 1][kMax_N + 1][kMax_N + 1];

int w, h;

bool dfs(int ly, int lx, int ry, int rx) {
  if (fieldL[ly][lx] == fieldR[ry][rx] && fieldL[ly][lx] == '%') {
    return true;
  }
  if (checked[ly][lx][ry][rx]) {
    return false;
  }

  checked[ly][lx][ry][rx] = true;

  int dyL[] = {-1, 0, 1, 0};
  int dxL[] = {0, 1, 0, -1};
  int dyR[] = {-1, 0, 1, 0};
  int dxR[] = {0, -1, 0, 1};

  bool ret = false;
  for (int i = 0; i < 4; i++) {
    int nly = ly + dyL[i];
    int nlx = lx + dxL[i];
    int nry = ry + dyR[i];
    int nrx = rx + dxR[i];
    if (nly < 0)
      nly = 0;
    if (nly >= h)
      nly = h - 1;
    if (nlx < 0)
      nlx = 0;
    if (nlx >= w)
      nlx = w - 1;
    if (nry < 0)
      nry = 0;
    if (nry >= h)
      nry = h - 1;
    if (nrx < 0)
      nrx = 0;
    if (nrx >= w)
      nrx = w - 1;
    if (fieldL[nly][nlx] == '#') {
      nly = ly;
      nlx = lx;
    }
    if (fieldR[nry][nrx] == '#') {
      nry = ry;
      nrx = rx;
    }
    ret |= dfs(nly, nlx, nry, nrx);
  }

  return ret;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w + h == 0) {
      break;
    }
    int ly, lx, ry, rx;
    for (int i = 0; i < h; i++) {
      string strL, strR;
      cin >> strL >> strR;
      for (int j = 0; j < w; j++) {
        fieldL[i][j] = strL[j];
        fieldR[i][j] = strR[j];
        if (strL[j] == 'L') {
          ly = i;
          lx = j;
        }
        if (strR[j] == 'R') {
          ry = i;
          rx = j;
        }
      }
    }

    memset(checked, false, sizeof(checked));

    if (dfs(ly, lx, ry, rx)) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}