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

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct po {
  int lx, ly, rx, ry;
};
int w, h;
string mp[2][51];
bool ans, memo[51][51][51][51], visited[51][51][51][51];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void saiki(int lx, int ly, int rx, int ry) {

  if (ans)
    return;
  if (mp[0][ly][lx] == '%' && mp[1][ry][rx] == '%')
    ans = 1;
  if (memo[lx][ly][rx][ry] || mp[0][ly][lx] == '%' || mp[1][ry][rx] == '%')
    return;
  memo[lx][ly][rx][ry] = 1;
  for (int i = 0; i < 4; i++) {
    int flg = 0;
    int nlx = lx + dx[i], nly = ly + dy[i];
    int nrx = rx - dx[i], nry = ry + dy[i];
    if (nlx < 0 || nly < 0 || nlx >= w || nly >= h || mp[0][nly][nlx] == '#')
      nlx = lx, nly = ly, flg++;
    if (nrx < 0 || nry < 0 || nrx >= w || nry >= h || mp[1][nry][nrx] == '#')
      nrx = rx, nry = ry, flg++;
    if (flg == 2)
      continue;
    saiki(nlx, nly, nrx, nry);
  }
}

bool bfs(int a, int b, int c, int d) {
  queue<po> Q;
  Q.push((po){a, b, c, d});
  while (!Q.empty()) {
    po t = Q.front();
    Q.pop();
    // cout << t.lx<<" "<<t.ly<<" "<<t.rx<<" "<<t.ry<<endl;
    if (mp[0][t.ly][t.lx] == '%' && mp[1][t.ry][t.rx] == '%')
      return true;
    if (visited[t.lx][t.ly][t.rx][t.ry] || mp[0][t.ly][t.lx] == '%' ||
        mp[1][t.ry][t.rx] == '%')
      continue;
    visited[t.lx][t.ly][t.rx][t.ry] = 1;
    for (int i = 0; i < 4; i++) {
      int flg = 0;
      int nlx = t.lx + dx[i], nly = t.ly + dy[i];
      int nrx = t.rx - dx[i], nry = t.ry + dy[i];
      if (nlx < 0 || nly < 0 || nlx >= w || nly >= h || mp[0][nly][nlx] == '#')
        nlx = t.lx, nly = t.ly, flg++;
      if (nrx < 0 || nry < 0 || nrx >= w || nry >= h || mp[1][nry][nrx] == '#')
        nrx = t.rx, nry = t.ry, flg++;
      if (flg == 2 || visited[nlx][nly][nrx][nry])
        continue;
      Q.push((po){nlx, nly, nrx, nry});
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[0][i] >> mp[1][i];
    int lx, ly, rx, ry;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (mp[0][i][j] == 'L')
          lx = j, ly = i;
        if (mp[1][i][j] == 'R')
          rx = j, ry = i;
      }
    memset(memo, 0, sizeof(memo));
    memset(visited, 0, sizeof(visited));
    ans = 0;
    // saiki(lx,ly,rx,ry);
    if (bfs(lx, ly, rx, ry))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int w, h;
string mp[2][51];
bool ans, memo[51][51][51][51];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void saiki(int lx, int ly, int rx, int ry) {
  if (ans)
    return;
  if (mp[0][ly][lx] == '%' && mp[1][ry][rx] == '%')
    ans = 1;
  if (memo[lx][ly][rx][ry] || mp[0][ly][lx] == '%' || mp[1][ry][rx] == '%')
    return;
  memo[lx][ly][rx][ry] = 1;
  for (int i = 0; i < 4; i++) {
    int flg = 0;
    int nlx = lx + dx[i], nly = ly + dy[i];
    int nrx = rx - dx[i], nry = ry + dy[i];
    if (nlx < 0 || nly < 0 || nlx >= w || nly >= h || mp[0][nly][nlx] == '#')
      nlx = lx, nly = ly, flg++;
    if (nrx < 0 || nry < 0 || nrx >= w || nry >= h || mp[1][nry][nrx] == '#')
      nrx = rx, nry = ry, flg++;
    if (flg == 2)
      continue;
    saiki(nlx, nly, nrx, nry);
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[0][i] >> mp[1][i];
    int lx, ly, rx, ry;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (mp[0][i][j] == 'L')
          lx = j, ly = i;
        if (mp[1][i][j] == 'R')
          rx = j, ry = i;
      }
    memset(memo, 0, sizeof(memo));
    ans = 0;
    saiki(lx, ly, rx, ry);
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}