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

#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
const int inf = 1e9;
int w, h;
int ldx[] = {-1, 0, 1, 0};
int ldy[] = {0, 1, 0, -1};
int rdx[] = {1, 0, -1, 0};
int rdy[] = {0, 1, 0, -1};
bool vis[55][55][55][55];
string rin[55];
string len[55];
bool treasure;
int rinsx, rinsy, ringx, ringy, lensx, lensy, lengx, lengy;
struct P {
  int ly, lx, ry, rx;
  P(int ly, int lx, int ry, int rx) : ly(ly), lx(lx), ry(ry), rx(rx){};
};
void bfs(int lx, int ly, int rx, int ry) {
  vis[ly][lx][ry][rx] = true;
  // lenvis[ly][lx]=true;
  // rinvis[ry][rx]=true;
  queue<P> q;
  q.push(P(ly, lx, ry, rx));
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int lennx, lenny, rinnx, rinny;
    bool lengoal = false;
    bool ringoal = false;
    for (int k = 0; k < 4; k++) {
      lennx = now.lx + ldx[k];
      lenny = now.ly + ldy[k];
      rinnx = now.rx + rdx[k];
      rinny = now.ry + rdy[k];
      if (lennx >= w || lennx < 0 || lenny >= h || lenny < 0 ||
          len[lenny][lennx] == '#') {
        lennx = now.lx;
        lenny = now.ly;
      }
      if (rinnx >= w || rinnx < 0 || rinny >= h || rinny < 0 ||
          rin[rinny][rinnx] == '#') {
        rinnx = now.rx;
        rinny = now.ry;
      }
      if (vis[lenny][lennx][rinny][rinnx])
        continue;
      if (len[lenny][lennx] == '%')
        lengoal = true;
      else
        lengoal = false;
      if (rin[rinny][rinnx] == '%')
        ringoal = true;
      else
        ringoal = false;
      if (lengoal && ringoal) {
        treasure = true;
        return;
      }
      if ((lengoal || ringoal) && !(lengoal && ringoal))
        continue;
      q.push(P(lenny, lennx, rinny, rinnx));
      vis[lenny][lennx][rinny][rinnx] = true;
    }
  }
}

int main() {
  while (cin >> w >> h && w) {
    treasure = false;
    memset(vis, false, sizeof(vis));
    // memset(lenvis,false,sizeof(lenvis));
    // memset(rinvis,false,sizeof(rinvis));
    for (int i = 0; i < h; i++) {
      cin >> len[i] >> rin[i];
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (len[i][j] == 'L') {
          lensy = i;
          lensx = j;
        }
        if (rin[i][j] == 'R') {
          rinsy = i;
          rinsx = j;
        }
      }
    }
    bfs(lensx, lensy, rinsx, rinsy);
    if (treasure) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
const int inf = 1e9;
int w, h;
int ldx[] = {-1, 0, 1, 0};
int ldy[] = {0, 1, 0, -1};
int rdx[] = {1, 0, -1, 0};
int rdy[] = {0, 1, 0, -1};
bool rinvis[55][55];
bool renvis[55][55];
bool vis[55][55][55][55];
string rin[55];
string ren[55];
bool treasure;
int rinsx, rinsy, ringx, ringy, rensx, rensy, rengx, rengy;
void bfs(int lx, int ly, int rx, int ry) {
  vis[ly][lx][ry][rx] = true;
  // renvis[ly][lx]=true;
  // rinvis[ry][rx]=true;
  if (ren[ly][lx] == '%' && rin[ry][rx] == '%') {
    treasure = true;
    return;
  } else {
    int rennx, renny, rinnx, rinny;
    bool rengoal = false;
    bool ringoal = false;
    for (int k = 0; k < 4; k++) {
      rennx = lx + ldx[k];
      renny = ly + ldy[k];
      rinnx = rx + rdx[k];
      rinny = ry + rdy[k];
      if (rennx >= w || renny >= h || rennx < 0 || renny < 0 || rinnx >= w ||
          rinny >= h || rinnx < 0 || rinny < 0)
        continue;
      // if(renvis[renny][rennx] || rinvis[rinny][rinnx]) continue;
      if (ren[renny][rennx] == '#') {
        rennx = lx;
        renny = ly;
      }
      if (rin[rinny][rinnx] == '#') {
        rinnx = rx;
        rinny = ry;
      }
      if (vis[renny][rennx][rinny][rinnx])
        continue;
      if (ren[renny][rennx] == '%')
        rengoal = true;
      else
        rengoal = false;
      if (rin[rinny][rinnx] == '%')
        ringoal = true;
      else
        ringoal = false;
      if (rengoal ^ ringoal)
        continue;
      else
        bfs(rennx, renny, rinnx, rinny);
    }
    return;
  }
}

int main() {
  while (cin >> w >> h && w) {
    treasure = false;
    memset(vis, false, sizeof(vis));
    // memset(renvis,false,sizeof(renvis));
    // memset(rinvis,false,sizeof(rinvis));
    for (int i = 0; i < h; i++) {
      cin >> ren[i] >> rin[i];
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < h; j++) {
        if (ren[i][j] == 'L') {
          rensy = i;
          rensx = j;
        }
        if (ren[i][j] == '%') {
          rengy = i;
          rengx = j;
        }
        if (rin[i][j] == 'R') {
          rinsy = i;
          rinsx = j;
        }
        if (rin[i][j] == '%') {
          ringy = i;
          ringx = j;
        }
      }
    }
    bfs(rensx, rensy, rinsx, rinsy);
    if (treasure) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}