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

#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << endl;

#define fi first
#define se second

#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;

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

struct POINT {
  int lx;
  int ly;
  int rx;
  int ry;
};

bool u[55][55][55][55] = {};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  //    ifstream in("in.txt");
  //    cin.rdbuf(in.rdbuf());
  int w, h;
  char ML[55][55], MR[55][55];
  int slx, sly, srx, sry;
  while (cin >> w >> h, w) {
    memset(u, 0, sizeof(u));
    memset(ML, 0, sizeof(ML));
    memset(MR, 0, sizeof(MR));

    rep(y, h) {
      string sml, smr;
      cin >> sml >> smr;
      sml.copy(ML[y], sml.size() + 1);
      smr.copy(MR[y], smr.size() + 1);
    }
    rep(y, h) rep(x, w) {
      if (ML[y][x] == 'L')
        ML[y][x] = '.', slx = x, sly = y;
      if (MR[y][x] == 'R')
        MR[y][x] = '.', srx = x, sry = y;
    }
    stack<POINT> st;
    st.push(POINT{slx, sly, srx, sry});

    bool flag = false;
    while (!st.empty()) {
      POINT p = st.top();
      st.pop();

      rep(i, 4) {
        int nlx = p.lx + dx[i];
        int nly = p.ly + dy[i];
        int nrx = p.rx + dx[(i + 2) % 4];
        int nry = p.ry + dy[i];
        bool left_do = true;
        bool right_do = true;
        if (nlx < 0 || w <= nlx)
          left_do = false;
        if (nly < 0 || h <= nly)
          left_do = false;
        if (ML[nly][nlx] == '#')
          left_do = false;
        if (nrx < 0 || w <= nrx)
          right_do = false;
        if (nry < 0 || h <= nry)
          right_do = false;
        if (MR[nry][nrx] == '#')
          right_do = false;
        if (ML[nly][nlx] == '%' && MR[nry][nrx] == '%') {
          flag = true;
          while (!st.empty())
            st.pop();
          break;
        } else if (ML[nly][nlx] == '%' || MR[nry][nrx] == '%') {
          continue;
        }
        if (!left_do && !right_do)
          continue;
        if (!left_do)
          nlx = p.lx, nly = p.ly;
        if (!right_do)
          nrx = p.rx, nry = p.ry;
        if (!u[nlx][nly][nrx][nry]) {
          u[nlx][nly][nrx][nry] = true;
          st.push(POINT{nlx, nly, nrx, nry});
        }
      }
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << endl;

#define fi first
#define se second

#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

struct POINT {
  int lx;
  int ly;
  int rx;
  int ry;
};

bool u[55][55][55][55] = {};
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  //    ifstream in("./input.txt");
  //    cin.rdbuf(in.rdbuf());
  int w, h;
  string ML[55], MR[55];
  int slx, sly, srx, sry;
  while (cin >> w >> h, w) {
    memset(u, 0, sizeof(u));
    rep(y, h) cin >> ML[y] >> MR[y];
    rep(y, h) rep(x, w) {
      if (ML[y][x] == 'L')
        ML[y][x] = '.', slx = x, sly = y;
      if (MR[y][x] == 'R')
        ML[y][x] = '.', srx = x, sry = y;
    }
    stack<POINT> st;
    st.push(POINT{slx, sly, srx, sry});

    bool flag = false;
    while (!st.empty()) {
      POINT p = st.top();
      st.pop();
      if (u[p.lx][p.ly][p.rx][p.ry])
        continue;
      rep(i, 4) {
        int nlx = p.lx + dx[i];
        int nly = p.ly + dy[i];
        int nrx = p.rx + dx[(i + 2) % 4];
        int nry = p.ry + dy[i];
        bool left_do = true;
        bool right_do = true;
        if (nlx < 0 || w <= nlx)
          left_do = false;
        if (nly < 0 || h <= nly)
          left_do = false;
        if (ML[nly][nlx] == '#')
          left_do = false;
        if (nrx < 0 || w <= nrx)
          right_do = false;
        if (nry < 0 || h <= nry)
          right_do = false;
        if (MR[nry][nrx] == '#')
          right_do = false;
        if (ML[nly][nlx] == '%' && MR[nry][nrx] == '%') {
          flag = true;
          while (!st.empty())
            st.pop();
          break;
        } else if (ML[nly][nlx] == '%' || MR[nry][nrx] == '%') {
          continue;
        }
        if (!left_do && !right_do)
          continue;
        if (!left_do)
          nlx = p.lx, nly = p.ly;
        if (!right_do)
          nrx = p.rx, nry = p.ry;
        if (!u[p.lx][p.ly][p.rx][p.ry])
          st.emplace(POINT{nlx, nly, nrx, nry});
      }
      u[p.lx][p.ly][p.rx][p.ry] = true;
    }
    if (flag)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}