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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef pair<int, int> P;
typedef pair<P, P> PP;
bool vis[50][50][50][50];
int H, W, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
string sl[50], sr[50];
bool okl(int x, int y) {
  return 0 <= x && x < H && 0 <= y && y < W && sl[x][y] != '#';
}
bool okr(int x, int y) {
  return 0 <= x && x < H && 0 <= y && y < W && sr[x][y] != '#';
}
void bfs(int slx, int sly, int srx, int sry) {
  queue<PP> que;
  que.push(PP(P(slx, sly), P(srx, sry)));
  vis[slx][sly][srx][sry] = 1;
  while (!que.empty()) {
    PP pp = que.front();
    que.pop();
    int lx = pp.fs.fs, ly = pp.fs.sc, rx = pp.sc.fs, ry = pp.sc.sc;
    if (sl[lx][ly] == '%' || sr[rx][ry] == '%')
      continue;
    rep(di, 4) {
      int lnx = lx + dx[di], lny = ly + dy[di], rnx = rx + dx[di],
          rny = ry + dy[di];
      if (!okl(lnx, lny))
        lnx = lx, lny = ly;
      if (!okr(rnx, rny))
        rnx = rx, rny = ry;
      if (vis[lnx][lny][rnx][rny])
        continue;
      vis[lnx][lny][rnx][rny] = 1;
      que.push(PP(P(lnx, lny), P(rnx, rny)));
    }
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    rep(i, H) {
      cin >> sl[i] >> sr[i];
      reverse(all(sr[i]));
    }
    int lx, ly, rx, ry, ax, ay, bx, by;
    rep(i, H) rep(j, W) if (sl[i][j] == 'L') lx = i, ly = j;
    rep(i, H) rep(j, W) if (sr[i][j] == 'R') rx = i, ry = j;
    rep(i, H) rep(j, W) if (sl[i][j] == '%') ax = i, ay = j;
    rep(i, H) rep(j, W) if (sr[i][j] == '%') bx = i, by = j;
    rep(i, H) rep(j, W) rep(k, H) rep(h, W) vis[i][j][k][h] = 0;
    bfs(lx, ly, rx, ry);
    if (vis[ax][ay][bx][by])
      puts("Yes");
    else
      puts("No");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef pair<int, int> P;
typedef pair<P, P> PP;
bool vis[50][50][50][50];
int H, W, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
string sl[50], sr[50];
bool okl(int x, int y) {
  return 0 <= x && x < H && 0 <= y && y < W && sl[x][y] != '#';
}
bool okr(int x, int y) {
  return 0 <= x && x < H && 0 <= y && y < W && sr[x][y] != '#';
}
void bfs(int lx, int ly, int rx, int ry) {
  queue<PP> que;
  que.push(PP(P(lx, ly), P(rx, ry)));
  while (!que.empty()) {
    PP pp = que.front();
    que.pop();
    int lx = pp.fs.fs, ly = pp.fs.sc, rx = pp.sc.fs, ry = pp.sc.sc;
    vis[lx][ly][rx][ry] = 1;
    if (sl[lx][ly] == '%' || sr[rx][ry] == '%')
      continue;
    rep(di, 4) {
      int lnx = lx + dx[di], lny = ly + dy[di], rnx = rx + dx[di],
          rny = ry + dy[di];
      if (!okl(lnx, lny))
        lnx = lx, lny = ly;
      if (!okr(rnx, rny))
        rnx = rx, rny = ry;
      if (vis[lnx][lny][rnx][rny])
        continue;
      que.push(PP(P(lnx, lny), P(rnx, rny)));
    }
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    rep(i, H) {
      cin >> sl[i] >> sr[i];
      reverse(all(sr[i]));
    }
    int lx, ly, rx, ry, ax, ay, bx, by;
    rep(i, H) rep(j, W) if (sl[i][j] == 'L') lx = i, ly = j;
    rep(i, H) rep(j, W) if (sr[i][j] == 'R') rx = i, ry = j;
    rep(i, H) rep(j, W) if (sl[i][j] == '%') ax = i, ay = j;
    rep(i, H) rep(j, W) if (sr[i][j] == '%') bx = i, by = j;
    rep(i, H) rep(j, W) rep(k, H) rep(h, W) vis[i][j][k][h] = 0;
    bfs(lx, ly, rx, ry);
    if (vis[ax][ay][bx][by])
      puts("Yes");
    else
      puts("No");
  }
}