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

#include <iostream>
#include <queue>
#define M 52
#include <cstring>
using namespace std;
typedef pair<int, int> P;
bool flag;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}, w, h;
char r[M][M], l[M][M];
bool dp[M][M][M][M];

void bfs(int a, int b, int c, int d) {
  queue<P> rr, ll;
  ll.push(P(a, b));
  rr.push(P(c, d));
  while (!ll.empty() && !flag) {
    P p = ll.front(), q = rr.front();
    ll.pop();
    rr.pop();
    int ly = p.first, lx = p.second, ry = q.first, rx = q.second;
    if (dp[ly][lx][ry][rx] != 0)
      continue;
    dp[ly][lx][ry][rx] = 1;
    if (l[ly][lx] == '%' && r[ry][rx] == '%') {
      flag = true;
      break;
    } else if (l[ly][lx] == '%' || r[ry][rx] == '%')
      continue;
    int nrx, nry, nlx, nly;
    for (int i = 0; i < 4; i++) {
      nrx = dx[i] + rx;
      nry = dy[i] + ry;
      nlx = -dx[i] + lx;
      nly = dy[i] + ly;
      if (!(nrx >= 0 && nrx < w && nry >= 0 && nry < h) || r[nry][nrx] == '#') {
        nrx = rx;
        nry = ry;
      }
      if (!(nlx >= 0 && nlx < w && nly >= 0 && nly < h) || l[nly][nlx] == '#') {
        nlx = lx;
        nly = ly;
      }
      if (dp[nly][nlx][nry][nrx] == 0) {
        ll.push(P(nly, nlx));
        rr.push(P(nry, nrx));
      }
    }
  }
}
int main() {
  int a, b, c, d;
  while (cin >> w >> h, h || w) {
    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') {
          a = i;
          b = j;
        }
        if (r[i][j] == 'R') {
          c = i;
          d = j;
        }
      }
    memset(dp, 0, sizeof(dp));
    flag = false;
    bfs(a, b, c, d);
    puts(flag ? "Yes" : "No");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define M 52
#include <cstring>
using namespace std;
bool flag;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}, w, h;
char r[M][M], l[M][M];
bool dp[M][M][M][M];
void func(int ly, int lx, int ry, int rx) {
  if (flag)
    return;
  // cout<<ly<<' '<<lx<<' '<<ry<<' '<<rx<<endl;
  if (dp[ly][lx][ry][rx] != 0)
    return;
  dp[ly][lx][ry][rx] = 1;
  int nrx, nry, nlx, nly;
  for (int i = 0; i < 4; i++) {
    nrx = dx[i] + rx;
    nry = dy[i] + ry;
    nlx = -dx[i] + lx;
    nly = dy[i] + ly;
    if (!(nrx >= 0 && nrx < w && nry >= 0 && nry < h) || r[nry][nrx] == '#') {
      nrx = rx;
      nry = ry;
    }
    if (!(nlx >= 0 && nlx < w && nly >= 0 && nly < h) || l[nly][nlx] == '#') {
      nlx = lx;
      nly = ly;
    }
    if (l[nly][nlx] == '%' && r[nry][nrx] == '%') {
      flag = true;
      continue;
    } else if (l[nly][nlx] == '%' || r[nry][nrx] == '%')
      continue;
    if (dp[nly][nlx][nry][nrx] == 0)
      func(nly, nlx, nry, nrx);
  }
}
int main() {
  int a, b, c, d;
  while (cin >> w >> h, h || w) {
    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') {
          a = i;
          b = j;
        }
        if (r[i][j] == 'R') {
          c = i;
          d = j;
        }
      }
    memset(dp, 0, sizeof(dp));
    flag = false;
    func(a, b, c, d);
    puts(flag ? "Yes" : "No");
  }
  return 0;
}