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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int w, h;
string sl[50], sr[50];
bool vis[50][50][50][50];
P ls, rs, lg, rg;
int dxl[] = {1, 0, -1, 0};
int dxr[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

bool range(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; }

bool dfs(P l, P r) {
  deque<PP> q;
  q.pb(PP(l, r));
  while (!q.empty()) {
    PP p = q.back();
    q.pop_back();

    if (p.fi == lg && p.se == rg)
      return true;
    else if (p.fi == lg || p.se == rg)
      continue;

    vis[p.fi.fi][p.fi.se][p.se.fi][p.se.se] = true;
    rep(i, 4) {
      int lx = p.fi.se + dxl[i], ly = p.fi.fi + dy[i];
      if (!range(lx, ly) || (range(lx, ly) && sl[ly][lx] == '#'))
        lx = p.fi.se, ly = p.fi.fi;
      int rx = p.se.se + dxr[i], ry = p.se.fi + dy[i];
      if (!range(rx, ry) || (range(lx, ly) && sr[ry][rx] == '#'))
        rx = p.se.se, ry = p.se.fi;
      if (!vis[ly][lx][ry][rx]) {
        q.pb(PP(P(ly, lx), P(ry, rx)));
        vis[ly][lx][ry][rx] = true;
      }
    }
  }
  return false;
}

int main() {
  cin.sync_with_stdio(false);
  while (cin >> w >> h, w | h) {
    memset(vis, 0, sizeof(vis));

    rep(i, h) {
      cin >> sl[i] >> sr[i];
      rep(j, w) {
        if (sl[i][j] == 'L')
          ls = P(i, j);
        if (sr[i][j] == 'R')
          rs = P(i, j);
        if (sl[i][j] == '%')
          lg = P(i, j);
        if (sr[i][j] == '%')
          rg = P(i, j);
      }
    }

    cout << (dfs(ls, rs) ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int w, h;
string sl[50], sr[50];
bool vis[2500][2500];
P ls, rs, lg, rg;
int dxl[] = {1, 0, -1, 0};
int dxr[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

bool range(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; }

bool dfs(P l, P r) {
  if (l == lg && r == rg)
    return true;
  else if (l == lg || r == rg)
    return false;

  vis[l.fi * 50 + l.se][r.fi * 50 + r.se] = true;
  bool ok = false;
  rep(i, 4) {
    int lx = l.se + dxl[i], ly = l.fi + dy[i];
    if (!range(lx, ly) || (range(lx, ly) && sl[ly][lx] == '#'))
      lx = l.se, ly = l.fi;
    int rx = r.se + dxr[i], ry = r.fi + dy[i];
    if (!range(rx, ry) || (range(lx, ly) && sr[ry][rx] == '#'))
      rx = r.se, ry = r.fi;
    if (!vis[ly * 50 + lx][ry * 50 + rx]) {
      ok |= dfs(P(ly, lx), P(ry, rx));
    }
  }
  return ok;
}

int main() {
  cin.sync_with_stdio(false);
  while (cin >> w >> h, w | h) {
    memset(vis, 0, sizeof(vis));

    rep(i, h) {
      cin >> sl[i] >> sr[i];
      rep(j, w) {
        if (sl[i][j] == 'L')
          ls = P(i, j);
        if (sr[i][j] == 'R')
          rs = P(i, j);
        if (sl[i][j] == '%')
          lg = P(i, j);
        if (sr[i][j] == '%')
          rg = P(i, j);
      }
    }

    cout << (dfs(ls, rs) ? "Yes" : "No") << endl;
  }

  return 0;
}