Problem p01271 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v)                                                               \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]" << endl;
#define STOP assert(false);
#else
#define dump(x) ;
#define LINE ;
#define dumpV(v) ;
#define STOP ;
#endif

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.fi << ", " << a.se << ')';
  return out;
}
} // namespace std

int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
auto isOutOfRange = [](int h, int w, int H, int W) {
  return h < 0 || h >= H || w < 0 || w >= W;
};

int merge(int y, int x) { return y * 50 + x; }
int getX(int yx) { return yx % 50; }
int getY(int yx) { return yx / 50; }

bool used[2510][2510];

int main() {
  int H, W;
  while (cin >> W >> H) {
    if (H == 0 && W == 0)
      break;
    rep(i, 2510) rep(j, 2510) used[i][j] = false;
    vector<string> sl(H), sr(H);
    rep(i, H) cin >> sl[i] >> sr[i];
    int sxl, syl, gxl, gyl;
    int sxr, syr, gxr, gyr;
    rep(i, H) rep(j, W) {
      if (sl[i][j] == 'L')
        syl = i, sxl = j;
      if (sl[i][j] == '%')
        gyl = i, gxl = j;
      if (sr[i][j] == 'R')
        syr = i, sxr = j;
      if (sr[i][j] == '%')
        gyr = i, gxr = j;
    }
    int startL = merge(syl, sxl), goalL = merge(gyl, gxl),
        startR = merge(syr, sxr), goalR = merge(gyr, gxr);
    queue<pii> q;
    pii p;
    q.push({startL, startR});
    used[startL][startR] = true;
    bool f = false;
    while (!q.empty()) {
      p = q.front();
      q.pop();
      int xl = getX(p.fi), yl = getY(p.fi);
      int xr = getX(p.se), yr = getY(p.se);

      pii l = {xl, yl}, r = {xr, yr};
      dump(l);
      dump(r);

      rep(i, 4) {
        int nxl = xl + dx[i], nyl = yl + dy[i];
        int nxr = xr + dx[(i + 2) % 4], nyr = yr + dy[i];
        if (isOutOfRange(nyl, nxl, H, W) || sl[nyl][nxl] == '#')
          nyl = yl, nxl = xl;
        if (isOutOfRange(nyr, nxr, H, W) || sr[nyr][nxr] == '#')
          nyr = yr, nxr = xr;
        int nxtL = merge(nyl, nxl), nxtR = merge(nyr, nxr);
        if (nxtL == goalL && nxtR == goalR) {
          f = true;
          break;
        }
        if (nxtL == goalL || nxtR == goalR || used[nxtL][nxtR])
          continue;
        q.push({nxtL, nxtR});
        used[nxtL][nxtR] = true;
      }
      if (f)
        break;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[10010];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

vector<vector<string>> s(2, vector<string>(51));
map<pair<pii, pii>, int> m;

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

bool isOutOfRange(pii p, int w, int h) {
  return p.fi < 0 || p.fi >= h || p.se < 0 || p.se >= w;
}

int main() {
  while (1) {
    int w = in.nextInt(), h = in.nextInt();
    if (w == 0 && h == 0)
      break;
    rep(i, h) s[0][i] = in.next(), s[1][i] = in.next();
    m.clear();
    pii p[2];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[0][i][j] == 'L')
          p[0] = {i, j};
        if (s[1][i][j] == 'R')
          p[1] = {i, j};
      }
    }
    bool f = false;
    queue<pair<pii, pii>> q;
    q.push({p[0], p[1]});
    pair<int, int> t[2];
    while (!q.empty()) {
      p[0] = q.front().fi;
      p[1] = q.front().se;
      q.pop();
      // cout<<p[0].fi<<" "<<p[0].se<<" "<<p[1].fi<<" "<<p[1].se<<endl;
      for (int j = 0; j < 4; j++) {
        for (int i = 0; i < 2; i++) {
          t[i] = p[i];
          t[i].fi += dy[j];
          t[i].se += dx[i][j];
          if (isOutOfRange(t[i], w, h) || s[i][t[i].fi][t[i].se] == '#')
            t[i] = p[i];
        }
        if (m[{t[0], t[1]}] == 0) {
          m[{t[0], t[1]}] = 1;
          q.push({t[0], t[1]});
          if (s[0][t[0].fi][t[0].se] == '%' && s[1][t[1].fi][t[1].se] == '%') {
            f = true;
            break;
          }
        }
      }
    }
    printf("%s\\n", f ? "Yes" : "No");
  }
}