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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

struct POINT {
  int x, y;
  bool operator<(const POINT &t) const {
    if (x == t.x)
      return y < t.y;
    return x < t.x;
  }
  bool operator==(const POINT &t) const { return x == t.x && y == t.y; }
};

struct P {
  POINT l, r;
  bool operator<(const P &t) const {
    if (l == t.l)
      return r < t.r;
    return l < t.l;
  }
};

char g1[64][64], g2[64][64];

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    memset(g1, '#', sizeof(g1));
    memset(g2, '#', sizeof(g2));

    POINT L, R;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf(" %c", &g1[i][j]);
        if (g1[i][j] == 'L') {
          L = {i, j};
          g1[i][j] = '.';
        }
      }
      for (int j = w; j >= 1; j--) {
        scanf(" %c", &g2[i][j]);
        if (g2[i][j] == 'R') {
          R = {i, j};
          g2[i][j] = '.';
        }
      }
    }

    queue<P> que;
    static bool done[51][51][51][51] = {false};
    memset(done, 0, sizeof(done));
    bool res = false;
    for (que.push({L, R}); !que.empty(); que.pop()) {
      P p = que.front();

      // printf("%d %d %d %d\\n", p.l.x, p.l.y, p.r.x, p.r.y);

      if (done[p.l.x][p.l.y][p.r.x][p.r.y])
        continue;
      done[p.l.x][p.l.y][p.r.x][p.r.y] = true;

      if (g1[p.l.x][p.l.y] == '%' && g2[p.r.x][p.r.y] == '%') {
        res = true;
        break;
      }
      if (g1[p.l.x][p.l.y] == '%' || g2[p.r.x][p.r.y] == '%') {
        continue;
      }

      for (int dir = 0; dir < 4; dir++) {
        int ltx = p.l.x + dx[dir];
        int lty = p.l.y + dy[dir];
        int rtx = p.r.x + dx[dir];
        int rty = p.r.y + dy[dir];

        if (g1[ltx][lty] == '#') {
          ltx = p.l.x;
          lty = p.l.y;
        }
        if (g2[rtx][rty] == '#') {
          rtx = p.r.x;
          rty = p.r.y;
        }

        que.push({{ltx, lty}, {rtx, rty}});
      }
    }
    printf(res ? "Yes\\n" : "No\\n");
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

struct POINT {
  int x, y;
  bool operator<(const POINT &t) const {
    if (x == t.x)
      return y < t.y;
    return x < t.x;
  }
  bool operator==(const POINT &t) const { return x == t.x && y == t.y; }
};

struct P {
  POINT l, r;
  bool operator<(const P &t) const {
    if (l == t.l)
      return r < t.r;
    return l < t.l;
  }
};

char g1[64][64], g2[64][64];

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    memset(g1, '#', sizeof(g1));
    memset(g2, '#', sizeof(g2));

    POINT L, R;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf(" %c", &g1[i][j]);
        if (g1[i][j] == 'L') {
          L = {i, j};
          g1[i][j] = '.';
        }
      }
      for (int j = w; j >= 1; j--) {
        scanf(" %c", &g2[i][j]);
        if (g2[i][j] == 'R') {
          R = {i, j};
          g2[i][j] = '.';
        }
      }
    }

    queue<P> que;
    set<P> done;
    bool res = false;
    for (que.push({L, R}); !que.empty(); que.pop()) {
      P p = que.front();

      // printf("%d %d %d %d\\n", p.l.x, p.l.y, p.r.x, p.r.y);

      if (done.count(p))
        continue;
      done.insert(p);

      if (g1[p.l.x][p.l.y] == '%' && g2[p.r.x][p.r.y] == '%') {
        res = true;
        break;
      }
      if (g1[p.l.x][p.l.y] == '%' || g2[p.r.x][p.r.y] == '%') {
        continue;
      }

      for (int dir = 0; dir < 4; dir++) {
        int ltx = p.l.x + dx[dir];
        int lty = p.l.y + dy[dir];
        int rtx = p.r.x + dx[dir];
        int rty = p.r.y + dy[dir];

        if (g1[ltx][lty] == '#') {
          ltx = p.l.x;
          lty = p.l.y;
        }
        if (g2[rtx][rty] == '#') {
          rtx = p.r.x;
          rty = p.r.y;
        }

        que.push({{ltx, lty}, {rtx, rty}});
      }
    }
    printf(res ? "Yes\\n" : "No\\n");
  }

  return 0;
}