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

#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<char, char> pcc;
int W, H;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
pcc s1, s2, g1, g2;
bool R1[50][50], R2[50][50], memo[50][50][50][50];

bool dfs(pcc p1, pcc p2) {
  stack<pair<pcc, pcc>> st;
  st.push(make_pair(p1, p2));
  while (!st.empty()) {
    p1 = st.top().first, p2 = st.top().second;
    st.pop();
    for (int i = 0; i < 4; ++i) {
      // int x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2;
      pcc p1_ = p1, p2_ = p2;
      if (p1_.first + dx[i] < H && p1_.first + dx[i] >= 0 &&
          p1_.second + dy[i] < W && p1_.second + dy[i] >= 0 &&
          R1[p1_.first + dx[i]][p1_.second + dy[i]]) {
        p1_.first += dx[i];
        p1_.second += dy[i];
      }
      if (p2_.first + dx[i] < H && p2_.first + dx[i] >= 0 &&
          p2_.second - dy[i] < W && p2_.second - dy[i] >= 0 &&
          R2[p2_.first + dx[i]][p2_.second - dy[i]]) {
        p2_.first += dx[i];
        p2_.second -= dy[i];
      }
      if (p1_ == g1 || p2_ == g2) {
        memo[p1_.first][p1_.second][p2_.first][p2_.second] = true;
      } else if ((!memo[p1_.first][p1_.second][p2_.first][p2_.second]) &&
                 (!memo[p1_.first][p1_.second][p2_.first][p2_.second])) {
        memo[p1_.first][p1_.second][p2_.first][p2_.second] = true;
        st.push(make_pair(p1_, p2_));
      }
    }
  }
  return memo[g1.first][g1.second][g2.first][g2.second];
}

int main() {
  while (cin >> W >> H, W) {
    string r1, r2;
    for (int i = 0; i < H; ++i) {
      cin >> r1 >> r2;
      for (int j = 0; j < W; ++j) {
        if (r1[j] == 'L') {
          s1.first = i;
          s1.second = j;
        }
        if (r2[j] == 'R') {
          s2.first = i;
          s2.second = j;
        }
        if (r1[j] == '%') {
          g1.first = i;
          g1.second = j;
        }
        if (r2[j] == '%') {
          g2.first = i;
          g2.second = j;
        }
        if (r1[j] != '#')
          R1[i][j] = true;
        else
          R1[i][j] = false;
        if (r2[j] != '#')
          R2[i][j] = true;
        else
          R2[i][j] = false;
      }
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        for (int k = 0; k < H; ++k) {
          for (int l = 0; l < W; ++l) {
            memo[i][j][k][l] = false;
          }
        }
      }
    }

    if (dfs(s1, s2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
bool memo[50][50][50][50], R1[50][50], R2[50][50];
int W, H;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int sx1, sy1, sx2, sy2, gx1, gx2, gy1, gy2;

void rec(int x1, int y1, int x2, int y2) {
  for (int i = 0; i < 4; ++i) {
    int x1_ = x1, y1_ = y1, x2_ = x2, y2_ = y2;
    if (x1 + dx[i] < H && x1 + dx[i] >= 0 && y1 + dy[i] < W &&
        y1 + dy[i] >= 0 && R1[x1 + dx[i]][y1 + dy[i]]) {
      x1_ += dx[i];
      y1_ += dy[i];
    }
    if (x2 + dx[i] < H && x2 + dx[i] >= 0 && y2 - dy[i] < W &&
        y2 - dy[i] >= 0 && R2[x2 + dx[i]][y2 - dy[i]]) {
      x2_ += dx[i];
      y2_ -= dy[i];
    }
    if ((x1_ == gx1 && y1_ == gy1) || (x2_ == gx2 && y2_ == gy2)) {
      memo[x1_][y1_][x2_][y2_] = true;
    } else if (!memo[x1_][y1_][x2_][y2_]) {
      memo[x1_][y1_][x2_][y2_] = true;
      rec(x1_, y1_, x2_, y2_);
    }
  }
}

int main() {
  while (cin >> W >> H, W) {
    string r1, r2;
    for (int i = 0; i < H; ++i) {
      cin >> r1 >> r2;
      for (int j = 0; j < W; ++j) {
        if (r1[j] == 'L') {
          sx1 = i;
          sy1 = j;
        }
        if (r2[j] == 'R') {
          sx2 = i;
          sy2 = j;
        }
        if (r1[j] == '%') {
          gx1 = i;
          gy1 = j;
        }
        if (r2[j] == '%') {
          gx2 = i;
          gy2 = j;
        }
        if (r1[j] != '#')
          R1[i][j] = true;
        else
          R1[i][j] = false;
        if (r2[j] != '#')
          R2[i][j] = true;
        else
          R2[i][j] = false;
      }
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        for (int k = 0; k < H; ++k) {
          for (int l = 0; l < W; ++l) {
            memo[i][j][k][l] = false;
          }
        }
      }
    }
    memo[sx1][sy1][sx2][sy2] = true;
    rec(sx1, sy1, sx2, sy2);
    if (memo[gx1][gy1][gx2][gy2])
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}