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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
char l[52][52], r[52][52];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int lsx, lsy, rsx, rsy, lgx, lgy, rgx, rgy;
bool ischecked[52][52][52][52];

bool solve(int lx, int ly, int rx, int ry) {
  // cout << lx << " " << ly << " " << rx << " " << ry << endl;
  // ischecked[lx][ly][rx][ry] = true;
  // if(lx == lgx && ly == lgy && rx == rgx && ry == rgy) return true;
  // if((lx == lgx && ly == lgy) || (rx == rgx && ry == rgy)) return false;
  queue<int> rxqu, ryqu, lxqu, lyqu;
  rxqu.push(rx);
  ryqu.push(ry);
  lxqu.push(lx);
  lyqu.push(ly);
  while (!rxqu.empty()) {
    lx = lxqu.front();
    ly = lyqu.front();
    rx = rxqu.front();
    ry = ryqu.front();
    lxqu.pop();
    lyqu.pop();
    rxqu.pop();
    ryqu.pop();
    if (lx == lgx && ly == lgy && rx == rgx && ry == rgy)
      return true;
    if (ischecked[lx][ly][rx][ry])
      continue;
    ischecked[lx][ly][rx][ry] = true;
    if ((lx == lgx && ly == lgy) || (rx == rgx && ry == rgy))
      continue;
    for (int i = 0; i < 4; i++) {
      int nlx = lx + dx[i], nly = ly + dy[i];
      int nrx = rx + dx[i], nry = ry - dy[i];
      if (nlx < 0 || nlx >= h || nly < 0 || nly >= w) {
        nlx = lx;
        nly = ly;
      }
      if (nrx < 0 || nrx >= h || nry < 0 || nry >= w) {
        nrx = rx;
        nry = ry;
      }
      if (l[nlx][nly] == '#') {
        nlx = lx;
        nly = ly;
      }
      if (r[nrx][nry] == '#') {
        nrx = rx;
        nry = ry;
      }
      if ((nrx == rx && nry == ry && nlx == lx && nly == ly) ||
          ischecked[nlx][nly][nrx][nry])
        continue;
      rxqu.push(nrx);
      ryqu.push(nry);
      lxqu.push(nlx);
      lyqu.push(nly);
      // ischecked[nlx][nly][nrx][nry] = false;
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> l[i][j];
      }
      for (int j = 0; j < w; j++) {
        cin >> r[i][j];
      }
    }
    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++) {
            ischecked[i][j][k][l] = false;
          }
        }
        if (l[i][j] == 'L') {
          lsx = i;
          lsy = j;
        }
        if (l[i][j] == '%') {
          lgx = i;
          lgy = j;
        }
        if (r[i][j] == 'R') {
          rsx = i;
          rsy = j;
        }
        if (r[i][j] == '%') {
          rgx = i;
          rgy = j;
        }
      }
    }
    // ischecked[lsx][lsy][rsx][rsy] = true;
    if (solve(lsx, lsy, rsx, rsy))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
string l[52], r[52];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int lsx, lsy, rsx, rsy, lgx, lgy, rgx, rgy;
bool ischecked[52][52][52][52];

bool solve(int lx, int ly, int rx, int ry) {
  // cout << lx << " " << ly << " " << rx << " " << ry << endl;
  ischecked[lx][ly][rx][ry] = true;
  if (lx == lgx && ly == lgy && rx == rgx && ry == rgy)
    return true;
  if ((lx == lgx && ly == lgy) || (rx == rgx && ry == rgy))
    return false;
  for (int i = 0; i < 4; i++) {
    int nlx = lx + dx[i], nly = ly + dy[i];
    int nrx = rx + dx[i], nry = ry - dy[i];
    if (nlx < 0 || nlx >= h || nly < 0 || nly >= w) {
      nlx = lx;
      nly = ly;
    }
    if (nrx < 0 || nrx >= h || nry < 0 || nry >= w) {
      nrx = rx;
      nry = ry;
    }
    if (l[nlx][nly] == '#') {
      nlx = lx;
      nly = ly;
    }
    if (r[nrx][nry] == '#') {
      nrx = rx;
      nry = ry;
    }
    if ((nrx == rx && nry == ry && nlx == lx && nly == ly) ||
        ischecked[nlx][nly][nrx][nry])
      continue;
    if (solve(nlx, nly, nrx, nry))
      return true;
    // ischecked[nlx][nly][nrx][nry] = false;
  }
  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      cin >> l[i] >> r[i];
    }
    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++) {
            ischecked[i][j][k][l] = false;
          }
        }
        if (l[i][j] == 'L') {
          lsx = i;
          lsy = j;
        }
        if (l[i][j] == '%') {
          lgx = i;
          lgy = j;
        }
        if (r[i][j] == 'R') {
          rsx = i;
          rsy = j;
        }
        if (r[i][j] == '%') {
          rgx = i;
          rgy = j;
        }
      }
    }
    // ischecked[lsx][lsy][rsx][rsy] = true;
    if (solve(lsx, lsy, rsx, rsy))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}