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

#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
using namespace std;
typedef struct P {
  int rh, rw, lh, lw, d;
} P;
struct Order {
  bool operator()(P const &a, P const &b) const { return a.d > b.d; }
};

int W, H, i, j, rh, rw, lh, lw, f;
int rm[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int lm[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
bool dp[55][55][55][55];
char M[55][110] = {0}, t;

int main() {
  for (; scanf("%d%d\\n", &W, &H), W;) {
    memset(M, 0, sizeof(M));
    memset(dp, 0, sizeof(dp));
    for (i = 1; i <= H; i++)
      for (j = 1; j <= 2 * W + 2; j++) {
        t = getchar();
        if (t == 'R') {
          rh = i;
          rw = j;
          t = '.';
        }
        if (t == 'L') {
          lh = i;
          lw = j;
          t = '.';
        }
        M[i][j] = t;
      }
    priority_queue<P, vector<P>, Order> Q;
    P p = {rh, rw, lh, lw, 0};
    Q.push(p);
    for (f = 0; !Q.empty();) {
      p = Q.top();
      Q.pop();
      if (M[p.rh][p.rw] == '%' && M[p.lh][p.lw] == '%') {
        f = 1;
        break;
      }
      if (M[p.rh][p.rw] == '%' || M[p.lh][p.lw] == '%' || p.d == 1500)
        continue;
      for (i = 0; i < 4; i++) {
        P ins = {p.rh, p.rw, p.lh, p.lw, p.d + 1};
        if (M[p.rh + rm[i][0]][p.rw + rm[i][1]] == '.' ||
            M[p.rh + rm[i][0]][p.rw + rm[i][1]] == '%') {
          ins.rh += rm[i][0];
          ins.rw += rm[i][1];
        }
        if (M[p.lh + lm[i][0]][p.lw + lm[i][1]] == '.' ||
            M[p.lh + lm[i][0]][p.lw + lm[i][1]] == '%') {
          ins.lh += lm[i][0];
          ins.lw += lm[i][1];
        }
        if (!dp[ins.rh][ins.rw][ins.lh][ins.lw - W - 1]) {
          dp[ins.rh][ins.rw][ins.lh][ins.lw - W - 1] = 1;
          Q.push(ins);
        }
      }
    }
    puts(f ? "Yes" : "No");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
using namespace std;
typedef struct P {
  int rh, rw, lh, lw, d;
} P;
struct Order {
  bool operator()(P const &a, P const &b) const { return a.d > b.d; }
};

int W, H, i, j, rh, rw, lh, lw, f;
int rm[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int lm[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
bool dp[55][55][55][55][205];
char M[110][110] = {0}, t;

int main() {
  for (; scanf("%d%d\\n", &W, &H), W;) {
    memset(M, 0, sizeof(M));
    memset(dp, 0, sizeof(dp));
    for (i = 1; i <= H; i++)
      for (j = 1; j <= 2 * W + 2; j++) {
        t = getchar();
        if (t == 'R') {
          rh = i;
          rw = j;
          t = '.';
        }
        if (t == 'L') {
          lh = i;
          lw = j;
          t = '.';
        }
        M[i][j] = t;
      }
    priority_queue<P, vector<P>, Order> Q;
    P p = {rh, rw, lh, lw, 0};
    Q.push(p);
    for (f = 0; !Q.empty();) {
      p = Q.top();
      Q.pop();
      if (M[p.rh][p.rw] == '%' && M[p.lh][p.lw] == '%') {
        f = 1;
        break;
      }
      if (M[p.rh][p.rw] == '%' || M[p.lh][p.lw] == '%' || p.d == 105)
        continue;
      for (i = 0; i < 4; i++) {
        P ins = {p.rh, p.rw, p.lh, p.lw, p.d + 1};
        if (M[p.rh + rm[i][0]][p.rw + rm[i][1]] == '.' ||
            M[p.rh + rm[i][0]][p.rw + rm[i][1]] == '%') {
          ins.rh += rm[i][0];
          ins.rw += rm[i][1];
        }
        if (M[p.lh + lm[i][0]][p.lw + lm[i][1]] == '.' ||
            M[p.lh + lm[i][0]][p.lw + lm[i][1]] == '%') {
          ins.lh += lm[i][0];
          ins.lw += lm[i][1];
        }
        if (!dp[ins.rh][ins.rw][ins.lh][ins.lw - W - 1][ins.d]) {
          dp[ins.rh][ins.rw][ins.lh][ins.lw - W - 1][ins.d] = 1;
          Q.push(ins);
        }
      }
    }
    puts(f ? "YES" : "NO");
  }
  return 0;
}