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

#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};

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

int mp[50][50][50][50];

int main() {
  int W, H, lsx, lsy, rsx, rsy, lgx, lgy, rgx, rgy;
  while (cin >> W >> H, W | H) {
    vector<string> Rl(H), Rr(H);
    for (int i = 0; i < H; i++) {
      cin >> Rl[i] >> Rr[i];
      for (int j = 0; j < W; j++) {
        if (Rl[i][j] == 'L') {
          lsx = i;
          lsy = j;
        } else if (Rl[i][j] == '%') {
          lgx = i;
          lgy = j;
        }
      }
      for (int j = 0; j < W; j++) {
        if (Rr[i][j] == 'R') {
          rsx = i;
          rsy = j;
        } else if (Rr[i][j] == '%') {
          rgx = i;
          rgy = j;
        }
      }
    }
    queue<pair<P, P>> q;
    q.push(pair<P, P>(P(lsx, lsy), P(rsx, rsy)));
    mp[lsx][lsy][rsx][rsy] = 1;
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int lx = p.first.x, ly = p.first.y, rx = p.second.x, ry = p.second.y;
      for (int i = 0; i < 4; i++) {
        int ltx = lx + dx[i], lty = ly - dy[i], rtx = rx + dx[i],
            rty = ry + dy[i];
        if (ltx >= 0 && ltx < H && lty >= 0 && lty < W && Rl[ltx][lty] != '#' &&
            rtx >= 0 && rtx < H && rty >= 0 && rty < W && Rr[rtx][rty] != '#') {
          if ((Rl[ltx][lty] == '%') == (Rr[rtx][rty] == '%') &&
              mp[ltx][lty][rtx][rty] == 0) {
            mp[ltx][lty][rtx][rty] = 1;
            q.push(pair<P, P>(P(ltx, lty), P(rtx, rty)));
          }
        } else if (ltx >= 0 && ltx < H && lty >= 0 && lty < W &&
                   Rl[ltx][lty] != '#') {
          if (Rl[ltx][lty] != '%' && mp[ltx][lty][rx][ry] == 0) {
            mp[ltx][lty][rx][ry] = 1;
            q.push(pair<P, P>(P(ltx, lty), P(rx, ry)));
          }
        } else if (rtx >= 0 && rtx < H && rty >= 0 && rty < W &&
                   Rr[rtx][rty] != '#') {
          if (Rr[rtx][rty] != '%' && mp[lx][ly][rtx][rty] == 0) {
            mp[lx][ly][rtx][rty] = 1;
            q.push(pair<P, P>(P(lx, ly), P(rtx, rty)));
          }
        }
      }
    }
    cout << (mp[lgx][lgy][rgx][rgy] ? "Yes" : "No") << endl;
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        for (int k = 0; k < 50; k++) {
          for (int l = 0; l < 50; l++) {
            mp[i][j][k][l] = 0;
          }
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

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

struct XY {
  int x, y;
  XY() {}
  XY(int x, int y) : x(x), y(y) {}
};

using P = pair<XY, XY>;

bool operator<(const XY &l, const XY &r) {
  return l.x < r.x || (l.x == r.x && l.y < r.y);
}

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

int W, H;
bool isIn(const XY &p) { return p.x >= 0 && p.x < H && p.y >= 0 && p.y < W; }

int main() {
  while (cin >> W >> H, W | H) {
    vector<string> Rl(H), Rr(H);
    for (int i = 0; i < H; i++) {
      cin >> Rl[i] >> Rr[i];
    }
    P s, g;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (Rl[i][j] == 'L') {
          s.first.x = i;
          s.first.y = j;
        } else if (Rl[i][j] == '%') {
          g.first.x = i;
          g.first.y = j;
        }
        if (Rr[i][j] == 'R') {
          s.second.x = i;
          s.second.y = j;
        } else if (Rr[i][j] == '%') {
          g.second.x = i;
          g.second.y = j;
        }
      }
    }
    bool flag = true;
    set<P> se;
    se.insert(s);
    while (flag) {
      flag = false;
      auto tmp = se;
      for (auto p : tmp) {
        for (int i = 0; i < 4; i++) {
          XY tl = p.first, tr = p.second;
          tl.x += dx[i];
          tl.y -= dy[i];
          tr.x += dx[i];
          tr.y += dy[i];
          bool f1 = isIn(tl) && Rl[tl.x][tl.y] != '#';
          bool f2 = isIn(tr) && Rr[tr.x][tr.y] != '#';
          if (f1 && f2) {
            if ((Rl[tl.x][tl.y] == '%') && (Rr[tr.x][tr.y] == '%')) {
              cout << "Yes" << endl;
              goto End;
            }
            if (!(Rl[tl.x][tl.y] == '%') && !(Rr[tr.x][tr.y] == '%')) {
              if (!se.count(P(tl, tr))) {
                flag = true;
                se.insert(P(tl, tr));
              }
            }
          } else if (f1) {
            if ((Rl[tl.x][tl.y] == '%') &&
                (Rr[p.second.x][p.second.y] == '%')) {
              cout << "Yes" << endl;
              goto End;
            }
            if (!(Rl[tl.x][tl.y] == '%') &&
                !(Rr[p.second.x][p.second.y] == '%')) {
              if (!se.count(P(tl, p.second))) {
                flag = true;
                se.insert(P(tl, p.second));
              }
            }
          } else if (f2) {
            if ((Rl[p.first.x][p.first.y] == '%') && (Rr[tr.x][tr.y] == '%')) {
              cout << "Yes" << endl;
              goto End;
            }
            if (!(Rl[p.first.x][p.first.y] == '%') &&
                !(Rr[tr.x][tr.y] == '%')) {
              if (!se.count(P(p.first, tr))) {
                flag = true;
                se.insert(P(p.first, tr));
              }
            }
          }
        }
      }
    }
    cout << "No" << endl;
  End:;
  }
  return 0;
}