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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};
char vis[50][50][50][50];
/*
bool solve(int xl,int yl,int xr, int yr)
{


        if(vis[yl][xl][yr][xr]==1) return false;

        if(L[yl][xl]=='%' && R[yr][xr]=='%') return true;
        if(L[yl][xl]=='%' && R[yr][xr]!='%') return false;
        if(L[yl][xl]!='%' && R[yr][xr]=='%') return false;



        vis[yl][xl][yr][xr]=1;

        for(int i=0;i<4;i++){
                int nxl=xl+dx[i], nyl=yl+dy[i];
                int nxr=xr-dx[i], nyr=yr+dy[i];

                if(nxl<0 || w<=nxl || nyl<0 || h<=nyl ||
                   nxr<0 || w<=nxr || nyr<0 || h<=nyr ) continue;

                if(L[nyl][nxl]=='#'){
                        nxl-=dx[i]; nyl-=dy[i];
                }
                if(R[nyr][nxr]=='#'){
                        nxr+=dx[i]; nyr-=dy[i];
                }

                if(nxl<0 || w<=nxl || nyl<0 || h<=nyl ||
                   nxr<0 || w<=nxr || nyr<0 || h<=nyr ) continue;

                if(solve(nxl,nyl,nxr,nyr)) return true;

        }

        return false;

}
*/
class Pos {
public:
  pair<int, int> posL, posR;
  Pos(int xl, int yl, int xr, int yr) {
    posL = make_pair(xl, yl);
    posR = make_pair(xr, yr);
  }
};

int main() {

  int w, h;
  while (cin >> w >> h, w) {

    memset(vis, -1, sizeof(char) * 50 * 50 * 50 * 50);

    string L[100], R[100];
    for (int i = 0; i < h; i++) {
      cin >> L[i] >> R[i];
    }

    int sxl, syl, sxr, syr;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (L[i][j] == 'L') {
          sxl = j, syl = i;
        }
        if (R[i][j] == 'R') {
          sxr = j, syr = i;
        }
      }
    }

    bool ok = false;
    queue<Pos> q;
    q.push(Pos(sxl, syl, sxr, syr));
    while (!q.empty()) {
      Pos pos = q.front();
      q.pop();
      int xl = pos.posL.first, yl = pos.posL.second;
      int xr = pos.posR.first, yr = pos.posR.second;

      if (L[yl][xl] == '%' && R[yr][xr] == '%') {
        ok = true;
        break;
      } else if (L[yl][xl] == '%' || R[yr][xr] == '%')
        continue;

      if (vis[xl][yl][xr][yr] == 1)
        continue;

      vis[xl][yl][xr][yr] = 1;

      for (int i = 0; i < 4; i++) {
        int nxl = xl, nyl = yl;
        int nxr = xr, nyr = yr;

        if (0 <= nxl + dx[i] && nxl + dx[i] < w && 0 <= nyl + dy[i] &&
            nyl + dy[i] < h && L[nyl + dy[i]][nxl + dx[i]] != '#') {
          nxl += dx[i];
          nyl += dy[i];
        }
        if (0 <= nxr - dx[i] && nxr - dx[i] < w && 0 <= nyr + dy[i] &&
            nyr + dy[i] < h && R[nyr + dy[i]][nxr - dx[i]] != '#') {
          nxr -= dx[i];
          nyr += dy[i];
        }

        q.push(Pos(nxl, nyl, nxr, nyr));
      }
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> PP;

int w, h;
int sxl, syl, sxr, syr;
string L[100], R[100];

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int vis[50][50][50][50];

bool solve(int xl, int yl, int xr, int yr) {

  if (vis[yl][xl][yr][xr] == 1)
    return false;

  if (L[yl][xl] == '%' && R[yr][xr] == '%')
    return true;
  if (L[yl][xl] == '%' && R[yr][xr] != '%')
    return false;
  if (L[yl][xl] != '%' && R[yr][xr] == '%')
    return false;

  vis[yl][xl][yr][xr] = 1;

  for (int i = 0; i < 4; i++) {
    int nxl = xl + dx[i], nyl = yl + dy[i];
    int nxr = xr - dx[i], nyr = yr + dy[i];

    if (nxl < 0 || w <= nxl || nyl < 0 || h <= nyl || nxr < 0 || w <= nxr ||
        nyr < 0 || h <= nyr)
      continue;

    if (L[nyl][nxl] == '#') {
      nxl -= dx[i];
      nyl -= dy[i];
    }
    if (R[nyr][nxr] == '#') {
      nxr += dx[i];
      nyr -= dy[i];
    }

    if (nxl < 0 || w <= nxl || nyl < 0 || h <= nyl || nxr < 0 || w <= nxr ||
        nyr < 0 || h <= nyr)
      continue;

    if (solve(nxl, nyl, nxr, nyr))
      return true;
  }

  return false;
}

int main() {

  while (cin >> w >> h, w) {

    memset(vis, -1, sizeof(int) * 50 * 50 * 50 * 50);

    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++) {
        if (L[i][j] == 'L') {
          sxl = j, syl = i;
        }
        if (R[i][j] == 'R') {
          sxr = j, syr = i;
        }
      }
    }

    if (solve(sxl, syl, sxr, syr))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}